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

Ask A Question

Stats

  • Questions 240k
  • Answers 240k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I haven't seen the code for LoginForm. But I think… May 13, 2026 at 7:14 am
  • Editorial Team
    Editorial Team added an answer check out the Douglas Peucker Algorithm which is used to… May 13, 2026 at 7:14 am
  • Editorial Team
    Editorial Team added an answer Can't you just set the projectId in the model you're… May 13, 2026 at 7:14 am

Related Questions

Curious if anyone has opinions on which method would be better suited for asp.net
I'm not good at putting up a question, feel free to reword/retag this to
Lets say I have a lists of entities (for example, a list of TODO
I want to permanently add an item to the cache. I am using the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.