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

  • Home
  • SEARCH
  • 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 8931443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:07:01+00:00 2026-06-15T09:07:01+00:00

I am using the following code, and to me it seems that a race

  • 0

I am using the following code, and to me it seems that a race condition would never happen with code below. Or there is still a chance of race condition?

        List<Document> listFromCache = Cache[dataCacheName] as List<Document>;
        if (listFromCache != null)
        {
           //do something with listFromCache. **IS IT POSSIBLE** that listFromCache is 
                                              //NULL here
        }
        else
        {
             List<Document> list = ABC.DataLayer.GetDocuments();
             Cache.Insert(dataCacheName, list, null, DateTime.Now.AddMinutes(5), 
                          System.Web.Caching.Cache.NoSlidingExpiration);
        }

UPDATE:
Chris helped me solve this problem, but I just thought, I would share some details that would be be very helpful to others.

To completely avoid any race condition, I had to add a check within the true part, else I could end up with a List with zero count, if someone else clears it in Cache ( not remove the item, but just call Clear method on the List object in Cache) after my if has evaluated to TRUE. So then, I would not have any data within my true part of if in listFromCache object.

To overcome, this subtle RACE condition in my original code, I have to double check listFromCache in the true part as in code below, and then repopulate Cache with latest data.

Also, as Chris said, if someone else ‘removes’ the items from Cache by calling the method Cache.Remove, then listFromCache would not be affected, since the Garbage Collector will not remove the actual List object from HEAP memory because a variable called ‘listFromCache’ is still having a reference to it ( I have explained this in more detail in a comment under Chris’s answer post).

List<Document> listFromCache = Cache[dataCacheName] as List<Document>;
    if (listFromCache != null)
    {
      //OVERCOME A SUBTLE RACE CONDITION BY IF BELOW
      if( listFromCache == null || listFromCache.Count == 0)
      {
          List<Document> list = ABC.DataLayer.GetDocuments();
          Cache.Insert(dataCacheName, list, null, DateTime.Now.AddMinutes(5), 
                      System.Web.Caching.Cache.NoSlidingExpiration);
       }
       //NOW I AM SURE MY listFromCache contains true data
       //do something with listFromCache. **IS IT POSSIBLE** that listFromCache is 
                                          //NULL here
    }
    else
    {
         List<Document> list = ABC.DataLayer.GetDocuments();
         Cache.Insert(dataCacheName, list, null, DateTime.Now.AddMinutes(5), 
                      System.Web.Caching.Cache.NoSlidingExpiration);
    }
  • 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-06-15T09:07:02+00:00Added an answer on June 15, 2026 at 9:07 am

    No, it’s not possible in your comment that listFromCache will become null as it’s a local reference at that point. If the cache entry is nullified elsewhere, it doesn’t affect your local reference. However, you could possibly get a condition where you retrieved a null value, but while in the process of gathering the documents (ABC.DataLayer.GetDocuments()) another process has already done so and inserted the cache entry, at which point you overwrite it. (this may be perfectly acceptable for you, in which case, great!)

    You could try locking around it with a static object, but honestly, I’m not sure if that’ll work in an ASP.NET context. I don’t remember if Cache is shared across all ASP.NET processes (which IIRC, have different static contexts) or only shared within each single web worker. If the latter, the static lock will work fine.

    Just to demonstrate too:

    List<Document> listFromCache = Cache[dataCacheName] as List<Document>;
    if (listFromCache != null)
    {
        Cache.Remove(dataCacheName);
        //listFromCache will NOT be null here.
        if (listFromCache != null)
        {
            Console.WriteLine("Not null!"); //this will run because it's not null
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using following code I try to get updated list of checkbuttons' corresponding text values,
I'm using the following code to load a list of objects from XML using
I'm using a wordpress framework (WooFramework) that uses the following code to create a
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>
I am creating date using following code try { newdatetime = new DateTime(2012, 2,
I am using following code for showing a MessageBox with ok and cancel button.
I am using following code to show a spinning wheel: $(#loading) .hide() .ajaxStart(function(){ $(this).show();
I am using following code to get bitmap from url. This function is used
i am using following code. public class MyCount extends CountDownTimer { public MyCount(long millisInFuture,
I am using following code for inserting comments: <div id=2 class=789678> <div id=dynamic2> <span

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.