The reason for this blog is that we are starting to look at ways we can speed up the application we use at work. One of the ideas we came up with is Caching.
So I thought I would do some research into the types of caching in NET and how it is used:
Caching of a page. The inner working of this is caching the final rendering out of the page so that the server does not have to run through the page life cycle to generate the page again.
There are options to vary this cache using querystring parameters. Using the vary by param option will allow you to cache different versions of the page depending on params as well as only one version.
Example: <%@ OutputCache Duration=”60″ VaryByParam=”None” %>
This will cache the page for 60 minutes and will not vary the cache based on any query string parameters
The vary by param options can be used in the following ways:
//This will cache different versions of the page when the following parameters are in the querystring: Id and SourceId
<%@ OutputCache Duration=”60″ VaryByParam=”Id;SourceId” %>
//This will vary the cache for all variations of the querystring
<%@ OutputCache Duration=”60″ VaryByParam=”*” %>
This allows the user to load items into the cache and it will be accessable from any page on the site. The using of this is simple:
//Caching the variable valueToCache
Cache[“CacheKey”] = valueToCache;
//Loading the cached Variable
object valueToCache = Cache[“CacheKey”];
Caching with dependencies:
This is similar to the previous explanation of caching except there are exceptions on how the data is cached and when it is valid. So this can be used to cache a file until it changes so that when the file is changed it will renew the cache. The other main option is to invalidate the cache after certain time period.
//Caching with a file dependency
Cache.Insert(“CacheKey”, valueToCache, New CacheDependency(Server.MapPath(“file.xml”)))
//Caching with a time dependancy of 1 minute
Cache.Insert(“CacheKey”, valueToCache, null, DateTime.Now.AddMinutes(1), TimeSpan.Zero)
SQL Dependency Cache:
The SQL Dependency Caching works on the same principle as the previously discussed caching with dependencies. The options available with caching are polling and notification.
The notification option is where the database will notify the application when the cache needs to be refreshed. Full versions of SQL Server 2005 have notification.
The polling option is where the application polls the database to see whether the data needs to be refreshed. The polling option requires more set up than the notification but if you are using SQL server express then this is a good option.
To set up the server, you will need to set up the database to use polling. A good guide for this can be found here: http://www.asp.net/learn/data-access/tutorial-61-vb.aspx
The advantage of this cache, which is little – known to most .NET developers, is that any of your related components or classes in any satellite DLL to your process all have access to this same Cache since they are all running under the same App Domain. You can store or retrieve virtually any object in AppDomain with the following sample code:
myType =(myType) System.AppDomain.CurrentDomain.GetData(“mystuff”)
Session vs Caching:
I have included this last section due to the fact that some of the applications I have see use the session as a means of caching data for use later. The point is to show the differences of using the session compared with the Cache. Firstly the session is obviously only available for that session. Another interesting point is that the cache does not serialize objects.
It simply stores references to them. The only overhead is the name lookup, which unless you store thousands of objects is a very quick operation. The session on the other hand serialises objects so there may be some overhead.
I hope this is a useful guide. Let me know whether there is anything I have forgotten or should have included.