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 210381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:00:00+00:00 2026-05-11T18:00:00+00:00

I want a ASP.NET cache item to be recycled when a specific file is

  • 0

I want a ASP.NET cache item to be recycled when a specific file is touched, but the following code is not working:

                       HttpContext.Current.Cache.Insert(
                            "Key",
                            SomeObject,
                            new CacheDependency(Server.MapPath("SomeFile.txt")),
                            DateTime.MaxValue,
                            TimeSpan.Zero,
                            CacheItemPriority.High,
                            null);

“SomeFile.txt” does not seem to be checked when I’m hitting the cache, and modifying it does not cause this item to be invalidated.

What am I doing wrong?

  • 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-11T18:00:00+00:00Added an answer on May 11, 2026 at 6:00 pm

    Problem Solved:

    This was a unique and interesting problem, so I’m going to document the cause and solution here as an Answer, for future searchers.

    Something I left out in my question was that this cache insertion was happening in a service class implementing the singleton pattern.

    In a nutshell:

    public class Service 
    {
            private static readonly Service _Instance = new Service();
            static Service () { }
            private Service () { }
    
            public static Service Instance
            {
                get { return _Instance; }
            }
    
            // The expensive data that this service exposes      
            private someObject _data = null;
    
            public someObject Data
            {
                get
                {
                     if (_data == null)
                         loadData();
                     return _data;
                }
            }
    
    
            private void loadData()
            {
                _data = GetFromCache();
                if (_data == null)
                {
                     // Get the data from our datasource
                     _data = ExpensiveDataSourceGet();
    
                     // Insert into Cache
                     HttpContext.Current.Cache.Insert(etc);
                }
            }
    }
    

    It may be obvious to some, but the culprit here is lazy loading within the singleton pattern. I was so caught up thinking that the cache wasn’t being invalidated, that I forgot that the state of the singleton would be persisted for as long as the worker process was alive.

    Cache.Insert has an overload that allows you to specify a event handler for when the cache item is removed, my first test was to create a dummy handler and set a breakpoint within it. Once I saw that the cache was being cleared, I realized that "_data" was not being reset to null, so the next request to the singleton loaded the lazy loaded value.

    In a sense, I was double caching, though the singleton cache was very short lived, but long enough to be annoying.

    The solution?

     HttpContext.Current.Cache.Insert(
         "Key",
          SomeObject,
          new CacheDependency(Server.MapPath("SomeFile.txt")),
          DateTime.MaxValue,
          TimeSpan.Zero,
          CacheItemPriority.High,
          delegate(string key, object value, CacheItemRemovedReason reason)
          {
              _data = null;
          }
     );
    

    When the cache is cleared, the state within the singleton must also be cleared…problem solved.

    Lesson learned here? Don’t put state in a singleton.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to remove a specific item from the ASP.NET Cache in order to
I'm not talking about asp.net membership. For each logged user I want to cache
I am working in a project in ASP.NET. I want to cache the username
Currently ASP.NET MVC's OutputCache attribute has a huge downfall. If you want to cache
I'm working on an ASP.NET web application and I want to implement caching, so
How can I cache variables in ASP.NET on page level? That is, I want
I want to start logging when items get removed from my asp.net cache and
I want to use ASP.NET Output cache in my website. If i select Location=Any
I've figured out that ASP.net httpcontext cache object can help in increasing web application
From MSDN about the differences between Adding or Inserting an item the ASP.NET Cache:

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.