In the application I am currently working on, I have data that does not change very often. The web application has product catalog and affiliate data that displays on pretty much every page of the site. This data does not change all that often. Even if it does, it will change, at most, once a day. In order to save expensive database resources, this data is stored in the ASP.Net Global Cache.
On my company’s web site, we have thousands of affiliates that direct traffic to us. Based on the affiliate we change certain display elements on our web site. What I did is create a class that encapsulates all functions and data that we need to display for an affiliate. Once an instance this class is loaded for an affiliate, it is stored in the global cache. This saves us from having to go back to the database for the affilate's data each time we need it in the application. It is especially good for affiliates that send us thousands of users in a single hour. I also set up configuration settings that control how old the data in the Cache Object can get so that when the data is changed, it will refresh itself. In addition, since the web site has thousands of affiliates, it would be a memory hog to hold all the affilliates data in the Cache Object taking up memory. If an affiliate’s data is only used once a day, then it is ok to go the the database that one time to retrieve the data. The main goal I have for caching in my application is to prevent hundreds of requests for the same data at the same time. Therefore, I usually set the AFFILIATE_CACHE_TIMEOUT setting to 30 minutes or less. I also have a setting that can turn caching of affiliate data off completely.
Here is how the code looks for working with the Cache:
Dim myAffiliate as Affilate
Dim blnCacheAffiliateData As Boolean = ConfigurationSettings.AppSettings.Get("CACHE_AFFILIATE_DATA")If blnCacheAffiliateData Then
If IsNothing(Cache("Affiliate_" & intAffiliateID)) And blnCacheAffiliateData Then
Dim intAffiliateCacheInterval As Integer = ConfigurationSettings.AppSettings.Get _
("AFFILIATE_CACHE_TIMEOUT")
Cache.Insert("Affiliate_" & intAffiliateID, New Affiliate(intAffiliateID), Nothing, _
DateTime.Now.AddMinutes(intPartnerCacheInterval), Nothing)
End If
myAffiliate = Cache("Affiliate_" & intAffiliateID)
Else
myAffiliate = New Affiliate(intAffiliateID)
End If
The first thing that the code does is check if affiliate data caching is on. If it is, it checks if the Affiliate Object exists in the Cache Object for the affiliate with the specified id. If so, it just pulls the object out of the Cache Object and uses the data. If not it creates an instance of the class, using the affiliate’s id, and stores it in the Cache. As you can see, when the object is stored in the Cache, it is given an absolute expiration date using the Cache interval setting. If affiliate caching is turned off, an instance of the affiliate class is created and populated with data each time the page is loaded.
Some other things worth noting about the cache object….
You also have the option of setting a relative expiration date. If you use this option, the Cache Object will expire what you put in the Cache if it is not accessed for the amount of time you specify. Also, you can set priorities on the items you insert in the Cache and define functions that you want to be called when an item you insert in the Cache is removed. For more information, here is a link to the definition of the Cache Object’s insert method.
I have yet to produce performance numbers about how using the Cache performs versus not using the Cache. However, it is hard to imagine that using the Cache does not perform better than not using the Cache.