Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically re-populate the cache at expiry time

Tags:

c#

.net

caching

I currently cache the result of a method invocation.

The caching code follows the standard pattern: it uses the item in the cache if it exists, otherwise it calculates the result, caching it for future calls before returning it.

I would like to shield client code from cache misses (e.g. when the item has expired).

I am thinking of spawning a thread to wait for the lifetime of the cached object, to then run a supplied function to repopulate the cache when (or just before) the existing item expires.

Can anyone share any experience related to this? Does this sound like a sensible approach?

I'm using .NET 4.0.

like image 540
Ben Aston Avatar asked Sep 14 '10 17:09

Ben Aston


People also ask

How do I set the expiry for cache?

One of the way to Set cache expiration is by using . htaccess file. Below code will set expiration for it's respective file type, e.g. for CSS files expiration will be 14 days.

How long cache expired?

By default, each file automatically expires after 24 hours, but you can change the default behavior in two ways: To change the cache duration for all files that match the same path pattern, you can change the CloudFront settings for Minimum TTL, Maximum TTL, and Default TTL for a cache behavior.

What is cache expired?

The value of the Expires date/time can cause the following specific cache behavior: When the Expires date is equal to the Date header value, the response is considered to be expired. When a response has an Expires header field with a date/time that is in the future, then that response is considered "cacheable".


1 Answers

Since this is ASP.NET, the Cache.Insert() method allows you to specify a callback delegate.

Does this sound like a sensible approach?

Yes, the callback (and File-dependency) are supplied for exactly this kind of situation. You still have ro make a trade of between resources, latency and out-of-dateness.

like image 62
Henk Holterman Avatar answered Oct 11 '22 05:10

Henk Holterman