Caching & Application Context in ASP.NET

Most web applications will require to store objects temporarily in the memory across the requests and the most commonly used is the Session object. Session objects are good for storing small amounts of data, but it is user specific. Application object is a good candidate to store data that can be shared across all users irrespective of whether the user is logged in or not. But once again, the application object holds the data either until the data is explicitly removed or the w3p process gets restarted. You cannot set a dependency on the data to be invalidated. That’s where the ASP.NET Caching comes to the help. I’m not going to illustrate the use of ASP.NET Caching as there are many articles published in the internet. Rather, my focus, is to how to utilize the Application Context object to invalidate the cached data based on the dependency and reload them once again.

Usually, if I have to store some data in Application object, I do this in the Global.asax.cs


protected void Application_Start(object sender, EventArgs e)
{
AddUsersCollectionToCache();
}

private void AddUsersCollectionToCache()
{
NameValueCollection colUsers = GetData();
CacheItemRemovedCallback OnRemove = CacheItemRemovedCallback(this.CollectionRemovedCallback);
CacheDependency cacheDependency = new CacheDependency("Path to the xml file");
HttpRuntime.Cache.Insert("UsersCollection", colUsers, cacheDependency, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration, CacheItemPriority.Normal,   OnRemove);
HttpContext.Current.Cache["Users"] = colUsers;

}

public void CollectionRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
HttpRuntime.Cache["ClientLogCollectionRemoved"] = true.ToString();
AddLogCollectionToCache();
}

private NameValueCollection GetData()
{
// Get the data from the xml file.
}

The AddUsersCollectionToCache() method calls the GetData method to retrieve the users list from the xml file and stores it in a NameValueCollection object. Then we define a delegate and set it to the handler method to be called whenever the xml file get’s changed. This is the dependency that we set to invalidate the data in the cache and reload it.

The main point here is how we use the application context. If you notice, I have used ‘HttpContext.Current.Cache.’ object to get the Cache object in the AddUsersCollectionToCache method. At the same time, I’m using ‘HttpRuntime.Cache’ object in the CollectionRemovedCallback event handler. This is because, the ‘HttpContext.Current.Cache’ will fail as the CollectionRemovedCallback can be called anytime irrespective of whether a request is in process or not. Further the event handler is not invoked by the incoming request; it’s invoked when the underlying xml file gets changed. ‘HttpContext.Current’ property returns the HttpContext object which is valid only for the active request. The ‘HttpRuntime.Cache’ property returns the Cache object for that application, irrespective of whether there is an active request or not.

Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: