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.

2
  • 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 Commented Jan 29, 2016 at 7:09
  • 2
    Maybe your app-domain is recycled by IIS for any reason (stackoverflow.com/questions/302110/…) and thus starting up with an empty cache.
    – jlvaquero
    Commented Jan 29, 2016 at 9:18

2 Answers 2

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.

1
+50

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.

5
  • 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! Commented Jan 31, 2016 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 ;) Commented Feb 1, 2016 at 11:15
  • Yeah, mate! It was the hidden property 'idle timeout' which was causing me headaches. Commented Feb 3, 2016 at 15:42
  • I guess you pointed me to the right direction. So gz to your bounty. ;-) Commented Feb 3, 2016 at 19:42
  • Thanks Fabian, that's pretty generous of you mate :) Commented Feb 4, 2016 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.