6

I'm using the HttpContext.Current.Cache to cache expensive data queries with a CacheItemUpdateCallback.

The idea is that the user has to load the data once and then the updatecallback keeps the object updated every 30 minutes.

This is how I insert / overwrite the cache:

HttpContext.Current.Cache.Insert(cacheID, cachedObject, null,
                                  expiryTimeStamp,
                                  Cache.NoSlidingExpiration,
                                  updateCallBack);   

For test reasons I set the expiration date like this (update every 20 seconds):

var expiryTimeStamp = DateTime.Now.Add(new TimeSpan(0, 0, 20));

This all works fine. That means, it's updating the expensive object every 20 seconds - but only for about 25 minutes. Then the trigger 'updateCallBack' is not called anymore!

I guess the problem is that the IIS disposes the cache so the callback 'CacheItemUpdateCallBack' is not fired anymore...But that is only a guess.

That leads me to my question:

Is there any setting I have to set in app.config or in the IIS? Or what am I doing wrong?

EDIT: Furthermore, I don't understand why the Insert overload with CacheItemUpdateCallback does not have a parameter CacheItemPriority. However, the overload with CacheItemRemovedCallback does have a priority parameter. Maybe, if I could set the CacheItemPriority to 'High' it would already solve my issue.

EDIT: I found the issue: There is an idle timeout property for each application pool which was set to 20 minutes.

Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
  • Do you have the code of the updateCallback? the way you implement it can have an influence on how all this works. For example, "if the callback method throws an exception, ASP.NET suppresses the exception and removes the cached value"... Maybe you throw an exception after 25 minutes for some reason – Simon Mourier Jan 29 '16 at 07:09
  • 2
    Maybe your app-domain is recycled by IIS for any reason (http://stackoverflow.com/questions/302110/what-causes-an-application-pool-in-iis-to-recycle) and thus starting up with an empty cache. – jlvaquero Jan 29 '16 at 09:18

2 Answers2

2

The idle timeout of the application pool was set to 20 minutes (as per default). And because it was a test instance, there was no user keeping the website alive.

Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
1

If your guess turns out to be right about IIS expiring cache then this might be the setting that you would like to tweak.

But I guess if this is happening after exact 25 minutes every single time then you have a hidden configuration set somewhere because if you do not specify cache expiration then the items will be cached as long as the process is running or if there is a memory allocation emergency.

Rachit Pandey
  • 346
  • 2
  • 12
  • I've already tried that too - without any success. In my case I set it to one year though. I'm going to try out 6 days on Monday, thank you! – Fabian Bigler Jan 31 '16 at 11:32
  • Hey Fabian, due to lack of reputation I am commenting here. Just for my clarity as well as that of future googlers, the idle timeout was forcing your process to stop which ultimately resulted in clearing your cache from IIS. Right ? If so, then the idle timeout was indeed the hidden configuration ;) – Rachit Pandey Feb 01 '16 at 11:15
  • Yeah, mate! It was the hidden property 'idle timeout' which was causing me headaches. – Fabian Bigler Feb 03 '16 at 15:42
  • I guess you pointed me to the right direction. So gz to your bounty. ;-) – Fabian Bigler Feb 03 '16 at 19:42
  • Thanks Fabian, that's pretty generous of you mate :) – Rachit Pandey Feb 04 '16 at 13:07