Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to use CacheItemRemovedCallback?

Tags:

c#

asp.net-mvc

I have a large data set that is updated once a day. I am caching the results of an expensive query on that data but I want to update that cache each day. I am considering using CacheItemRemovedCallback to reload my cache on a daily interval, but I had the following concerns:

  1. Isn't it possible that the CacheItemRemovedCallback could be called before my expiration (in the case of running out of memory)? Which means reloading it immediately doesn't seem like a good idea.
  2. Does the CacheItemRemovedCallback get called before or after the item is actually removed? If it is after, doesn't this theoretically leave a period of time where the cache would be unavailable?

Are these concerns relevant and if using CacheItemRemovedCallback to reload your cache is a bad idea, then when is it useful?

like image 344
Code Commander Avatar asked Sep 17 '10 23:09

Code Commander


3 Answers

  1. If you're going to reload, be sure to check the CacheItemRemovedReason. I recently had to debug an issue where a developer decided they should immediately re-populate the cache in this method, and under low memory conditions, it basically sat chewing up CPU while it got stuck in a loop of building the cache objects, adding them to the cache, expiring, repeat.

  2. The callback is fired after the item is removed.

like image 174
Danny Tuppeny Avatar answered Nov 09 '22 23:11

Danny Tuppeny


From everyone's responses and from further reading I have come to the following conclusion:

My concerns are valid. Using CacheItemRemovedCallback to refresh cached items is not a good idea. The only practical use for this callback seems to be logging information about when your cache is removed.

It seems that CacheItemUpdateCallback is the more appropriate way of refreshing your cache on a regular interval.

Ultimately, I have decided not to use either of these calls. Instead I will write a service action so the database import job can notify my application when it needs to refresh its data. This avoids using a timed refresh altogether.

like image 33
Code Commander Avatar answered Nov 09 '22 22:11

Code Commander


  1. Yes, there is a change that the method could be fired off for a lot of various reasons. However, loading or waiting to load the cache again would be dependent upon what is best for your typical use case in your application.

  2. CacheItemRemovedCallback does indeed fire after the item is removed from the cache. Right before the item is to be removed, you can use the CacheItemUpateCallback method to determine whether or not you want to flush the cache at that time. There may be good reasons to wait in flushing the cache, such as you currently have users in your application and it takes a long amount of time to build the cache again.

Generally speaking, the best practice is to test that your cached item actually exists in the cache before using its data. If the data doesn't exist, you can rebuild the cache at that time (causing a slightly longer response for the user) or choose to do something else.

like image 1
Dillie-O Avatar answered Nov 09 '22 21:11

Dillie-O