Is there a pre-remove event in caching app block?

Topics: Caching Application Block
Apr 28, 2012 at 11:54 AM

I'm using caching application block in my project to store some data that is fetched aperiodically.

When the program reconize those data will not be used  within a long period, I want them removed after a 5 minutes delay, due to a possibility they might be marked as useful again within 5 minutes.

So I add them with 5 minutes expiration time.

The question is:

Is there a pre-remove event that use at least a bool parameter to allow my bussiness logic code determine whether the cache item should be removed or not?

Or can I add them as never expire items, then changing to 5 minues expiration as soon as they are reconized as useless?

Apr 30, 2012 at 3:40 AM

There is no pre-remove event.  There is an ICacheItemRefreshAction interface which allows you to hook into a post-remove event.

You can definitely re-add an item to the cache.  If another item already exists with the same key, that item is removed before the new item is added. The new item will use the new values that were specified when re-adding the item (e.g. expiration).  This means that you can add a new item to the cache set to NeverExpire (the default) and later in response to some event "update" the expirations (via the Add method) to use an AbsoluteTime expiration set 5 minutes in the future. 

Another approach would be write your own ICacheItemExpiration to implement your logic.  ICacheItemExpiration has a method, HasExpired, which the cache uses to determine if the item is expired so if you could somehow implement your custom logic in that method it would integrate with the Caching Application Block nicely.  However, it sounds like it might not be that feasible in your scenario. 

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 1, 2012 at 2:26 AM

I have tried refresh action before this post.

There is a multi-thread sync problem, when the refresh action is running, the cache item is not in the cache, and this happens every 5 minutes, so there is a prety high chance that GetData will return a null as I already encountered. I don't know what to lock and when to put the lock to solve the problem. Add an item with the same key might cause the same problem. Any suggestions on this?

 

If the sync problem can't be solved, I will try to implement my own ICacheItemExpiration as you suggested.


If the two approaches don't work with my scenario, I will be forced to add a pre-remove event in the cache app block as the last work around.

Did you mean that when I add an item whose key already exists in cache, the pre-remove event will also be triggered?

Would you please tell me anything else I should pay attention to when doing this?

May 1, 2012 at 4:05 AM

ICacheItemRefreshAction is processed on a thread pool thread so you can definitely hit some threading issues if you wish to interact with the cache from the ICacheItemRefreshAction method.

> Did you mean that when I add an item whose key already exists in cache, the pre-remove event will also be triggered?

If you follow the current implementation when adding the pre-remove event (to the Cache.Remove() method) then the pre-remove won't be invoked when adding an item whose key already exists in the cache.

You don't need to modify the caching block (of course you are free to do so) but you can implement your own ICacheManager instead (but you would probably be copying quite a bit of the Enterprise Library code).

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 1, 2012 at 1:24 PM

Thank you for reply.

I will try the approaches.