Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is cacheitemremovedcallback of HttpRuntime.Cache thread safe?

i use HttpRuntime.Cache.Insert to insert data into cache. i have the function 'onremove' as the cacheitemremovedcallback - when the cache expires (after 15 minutes) it releases data in cache and calls 'onremove' that insert the data again to the cache.

everytime i want to use the data in the cache i check that the data is there first:

if (HttpRuntime.Cache[CACHE_DATA_TABLE] == null)
            { // load data into cache again}

what happens if i check that the data is in the cache and it is there, but as soon as i want to use it it expires? so when i call:

DATADT = (DataTable)HttpRuntime.Cache[CACHE_DATA_TABLE]

Does the HttpRuntime.Cache waits untill 'onremove' is called and finish loading the data back into cache before it extract the data ?

like image 736
Rodniko Avatar asked Jan 29 '26 13:01

Rodniko


1 Answers

You should use a CacheItemUpdateCallback delegate instead.

This lets you take the desired action before the item is about to be removed.

See: MSDN on Cache.Insert Method (String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)

like image 174
universalist Avatar answered Jan 31 '26 07:01

universalist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!