Click here to Skip to main content
12,358,418 members (30,983 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

14K views
13 bookmarked
Posted

Caching in .NET

, 13 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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:

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:

<%@ 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=”*” %>

Caching

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);

//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 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

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:

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 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.

License

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

Share

About the Author

stephen.vaubell
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralCaching vary by control Pin
igorfrancobrum20-Jan-10 8:34
memberigorfrancobrum20-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 13 Jan 2010
Article Copyright 2010 by stephen.vaubell
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid