2
votes

I've created a web site that uses classic ASP.NET caching:

DataSet ds = new DataSet();
ds = (DataSet) Cache["MyDataSet"];

In ASP.NET code, I've set the duration for several hours, however IIS resets cache much earlier.
In IIS, where can I manually set the duration of ASP.NET caching?

I have enough RAM, with fully loaded cache and working sites I have about 1/3 free (3GBtotal, Web Server 2008R2).

(I have heavy calculations (several minutes) that are in cache, and during work I update only small parts of cache)

2
What does this statement mean: IIS resets cache much earlier? How have you come to the conclusion that IIS is resetting your ASPNET cache? What specific observations have you made that lead you to this conclusion? - Cheeso
Is your cache cleared or does IIS recycle - Pleun
What is your application pool recycle time? Cache normally cleared when ever application pool recycles - Emmanuel N

2 Answers

2
votes

When you add the object to the Cache, you can specify the expiration policy (absolute, sliding expiration).

Cache.Insert("CacheItem", yourobject,
    null, DateTime.Now.AddHours(3), 
    System.Web.Caching.Cache.NoSlidingExpiration);

See here for a more detailed explanation.

Here's some reasons why an object may be removed from Cache:

ASP.NET can remove data from the cache for one of these reasons:

Because memory on the server is low, a process known as scavenging.

Because the item in the cache has expired.

Because the item's dependency changes.

To help you manage cached items, ASP.NET can notify your application when items are removed from the cache.

Scavenging

Scavenging is the process of deleting items from the cache when memory is scarce. Items are removed when they have not been accessed in some time or when items are marked as low priority when they are added to the cache. ASP.NET uses the CacheItemPriority object to determine which items to scavenge first. For more information, see How to: Add Items to the Cache. Expiration

In addition to scavenging, ASP.NET automatically removes items from the cache when they expire.

0
votes

In order to test when your element gets automatically removed from your cache, you can get notified when that happens: ASP.NET provides the CacheItemRemovedCallback delegate for that purpose. The Cache.Insert statement has an overload, so you can write something like

Cache.Insert(
        "CacheItem",
        yourobject, 
        null,
        DateTime.Now.AddHours(3), 
        System.Web.Caching.Cache.NoSlidingExpiration
        CacheItemPriority.Default,
        new CacheItemRemovedCallback(ObjectRemovedCallback));

(not sure if I got that 100% right) and then have that handled by a ObjectRemovedCallback method in a business object. Check http://msdn.microsoft.com/en-us/library/7kxdx246.aspx for more details. There is a good chance that you can find out the reason for your premature cache expiration here.