find a location for property in a new city

Friday, 28 January 2011

Caching. Absolute expiration or Sliding scale expiration?

You can set a cache with a temporal expiration in one of two ways:

Absolute expiration

This mean you set a time in the future in which it will expire e.g. DateTime.Now.AddMinutes(5). So the cache will remove the object from memory at that particular time in the future.

Sliding scale expiration

This means you set a length of time that the cache should keep the object in memory for from the last time it was accessed e.g. new TimeSpan(0, 0, 5, 0). That is 5 mins. So the object will be removed from cache if it hasn’t been accessed for 5mins or more.

Which temporal caching method to use?

I believe Absolute expiration is the one to use for most web page cases, unless there is a good reason not to. Imagine you are using a 5 minute sliding scale cache expiration on a popular page that gets say 50 hits/sec, chances are that this object will never expire from the cache.

How long to cache for?

Also avoiding huge expirations of hours or days on not outrageously expensive data would be an idea too. Although the benefits of large expirations are that you have to retrieve the data less often I think that reducing it from 50*60*5 = 15000 requests/5mins down to 1 request/5mins is efficient enough, without suffering from stagnant data.

Obviously, the length of how long to set your expiration for and your method of expiration is will depend on how expensive your data is and possibly other specific requirements but hopefully you can now make a more informed decision.

Follow britishdev on Twitter

No comments:

Post a Comment