Click here to Skip to main content
15,886,639 members
Articles / .NET

Caching in .NET

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
13 Jan 2010CPOL3 min read 18.2K   13   1
Caching in .NET

The reason for this blog is that we are starting to look at ways in which 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:

Output Caching

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:

C#
<%@ 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:

XML
//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="*" %>

Caching

This allows the user to load items into the cache and it will be accessible from any page on the site. The using of this is simple:

C#
//Caching the variable valueToCache
Cache("CacheKey", valueToCache);

//Or
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 a certain time period.

C#
//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.

AppDomain Cache

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:

C#
System.AppDomain.CurrentDomain.SetData("mystuff", myType)
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 seen 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.

This article was originally posted at http://www.makecodingeasy.com?p=20

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCaching vary by control Pin
igorfrancobrum20-Jan-10 8:34
igorfrancobrum20-Jan-10 8:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.