Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6220749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:00:43+00:00 2026-05-24T08:00:43+00:00

I am caching data in an ASP.NET website through the System.Web.Caching.Cache-Class, because retrieving the

  • 0

I am caching data in an ASP.NET website through the System.Web.Caching.Cache-Class, because retrieving the data is very costly and it changes only once in a while, when our content people change data in the backend.

So I create the data in Application_Start and store it in Cache, with an expiration time of 1 day.

When accessing the data (happens on many pages of the website), I have something like this now in a static CachedData class:

public static List<Kategorie> GetKategorieTitelListe(Cache appCache)
{
    // get Data out of Cache
    List<Kategorie> katList = appCache[CachedData.NaviDataKey] as List<Kategorie>;
    // Cache expired, retrieve and store again
    if (katList == null)
    {
            katList = DataTools.BuildKategorienTitelListe();
            appCache.Insert(CachedData.NaviDataKey, katList, null, DateTime.Now.AddDays(1d), Cache.NoSlidingExpiration);
    }
    return katList;
}

The problem I see with this code is that its not threadsafe.
If two users open two of these pages at the same time and the cache just ran out, there is a risk the data while be retrieved multiple times.

But if I lock the method body, I will run into performance troubles, because only one user at a time can get the data list.

Is there an easy way to prevent this? What’s best practice for a case like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:00:46+00:00Added an answer on May 24, 2026 at 8:00 am

    You are right, your code is not thread safe.

    // this must be class level variable!!!
    private static readonly object locker = new object();
    
        public static List<Kategorie> GetKategorieTitelListe(Cache appCache)
        {
            // get Data out of Cache
            List<Kategorie> katList = appCache[CachedData.NaviDataKey] as List<Kategorie>;
    
            // Cache expired, retrieve and store again
            if (katList == null)
            {
                lock (locker)
                {
                    katList = appCache[CachedData.NaviDataKey] as List<Kategorie>;
    
                    if (katlist == null)  // make sure that waiting thread is not executing second time
                    {
                        katList = DataTools.BuildKategorienTitelListe();
                        appCache.Insert(CachedData.NaviDataKey, katList, null, DateTime.Now.AddDays(1d), Cache.NoSlidingExpiration);
                    }
                }
            }
            return katList;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm needing to cache some data using System.Web.Caching.Cache . Not sure if it matters,
Say, for example, you are caching data within your ASP.NET web app that isn't
We have a data driven ASP.NET website which has been written using the standard
I use partial page caching with ASP.NET. I find it to be particularly helpful
I have put together a small ASP.NET MVC 2 site that does some very
I am working on a web app in ASP.NET/C# which needs to be scalable
If I plan to use data caching do I have to worry about conflicts
What other caching frameworks are available for .NET besides the project codenamed Velocity. What
I am trying to put some distributed caching into play, I'm using this indeXus.Net
I'm trying to figure out the best way to do caching for a website

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.