HttpContext.Current.Cache - When does this gets cleared automatically?

后端 未结 2 431
离开以前
离开以前 2021-01-11 13:11

In my Web forms application I am using HttpContext.Current.Cache to store some information which different forms use to avoid going to db every time.

My Question is,

2条回答
  •  有刺的猬
    2021-01-11 13:55

    According to MSDN, the cache will be cleared on the following scenarios:

    1. The code manually clears an entry.
      • By using the Remove method of the Cache object.
    2. The cache entry expires.
      • By using the absoluteExpiration or slidingExpiration parameters of the Add and Insert methods of the Cache object.
    3. A dependency of the cached item is removed from the cache.
      • By defining a dependency through the use of the CacheDependency class when adding items to the cache.
      • Removing a dependency will remove/expire all dependent items.
    4. The host process ends (application or IIS reset, application pool recycle, etc).
    5. System memory becomes scarce (automatic - called scavenging - see below).

    Scavenging

    Even though cases 1 to 4 are mostly self-explanatory, the system memory availability scenario might not be evident at first. See the quote below from the MSDN article on caching application data.

    The Cache class offers powerful features that allow you to customize how items are cached and how long they are cached. For example, when system memory becomes scarce, the cache automatically removes seldom-used or low-priority items to free memory. This technique is referred to as scavenging, and is one of the ways that the cache ensures that out-of-date data does not consume valuable server resources.

    According to the same article, you can set priority levels on your cached items to try to secure your most important items from being automatically removed by using the CacheItemPriority enumeration.

    var expiresAt = DateTime.UtcNow.Date.AddHours(24);
    System.Web.HttpContext.Current.Cache.Insert(
        "myKey",
        myValue,
        null,
        expiresAt,
        Cache.NoSlidingExpiration,
        CacheItemPriority.High,
        null
    );
    

    Note that you also have the option of setting the priority to NotRemovable, which would disable the automatic removal of this item through scavenging. However, please note that this is not the default behavior.

    Also note that, if you use large amounts of memory through cache by setting the priority to NotRemovable, your application might throw run time memory availability errors.

    Information on how much of your system's memory is available for your application's cache can be retrieved through the property EffectivePercentagePhysicalMemoryLimit of the Cache object.

    Logging/Debugging

    If you wish to carefully understand the status of the cache on your application through logs (for instance), it's usually useful to look at the moments where the item is inserted and removed from the cache.

    Logging the moment where the item is inserted is usually pretty simple, since you only have to look for calls to the Add or Insert methods of the Cache object.

    However, since cached items may be automatically removed, to log the removal of an item, you should use the CacheItemRemovedCallback delegate type when inserting items to the cache.

    This can be really useful when debugging/improving legacy applications that rely heavily on caching data.

    See MSDN article below for more details.

    Sources:

    MSDN Articles:

    • Caching Application Data
    • How to: Notify an Application When an Item Is Removed from the Cache

    API:

    • Cache.Remove Method
    • Cache.Add Method
    • Cache.Insert Method
    • CacheDependency Class
    • CacheItemPriority Enumeration
    • Cache.EffectivePercentagePhysicalMemoryLimit Property

    I know this question is back from 2011. However, it took me some time to figure out a bug in a legacy application I was working on that was caused by the automatic clear up policy based on available physical memory.

    Hopefully, this answer will help someone else.

    And thanks to @J. Ed and @Oded for their answer and comments. They helped me get in the right track to find the issue.

提交回复
热议问题