Monday, April 16, 2007

 

Cache Access Pattern and updating it frequently

I came across a technique to effectively retrieve any object from the Cache with Steven Smith's Cache Access Pattern.

Say you need to access a DataTable from the Cache, you should first cast it into an object and then check if the object is not 'null'. You can then cast a DataTable from this object, therefore avoid hitting the cache twice.

Here is a link to a post from Scott Cate, where he describes how the Cache Access Pattern help solve a 'Object Not Set' exception issue on KbAlertz.com.

In addition to retrieving the objects from Cache, when a Cache object is updated frequently, another trick is to lock the Cache object. In the code below notice that we obtain a lock on the static string, this is much cheaper than locking the entire Cache object itself.

private static string lockString = "";
System.Web.Caching.Cache objCache = HttpContext.Current.Cache;

lock (lockString)
{
objCache.Insert(...);
}


For a high performance website, this is critical where multiple threads need to update the cache, we ensure that only one thread has write access to the Cache object while other threads wait before making an update.
Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?