Quantcast

ASP.NET: Server-side and Client-side Caching

Get the WebProNews Newsletter:
[ Business]

Caching is a very easy solution to many performance related issues on almost any website.

There are many different ways to use server-side caching and they all have their own unique advantages. But client-side caching is often ignored in the .NET blogosphere even though it is just as important as the server-side cache.

Everywhere server-side caching is used you can and should use client-side caching as well. Even though the server can serve cached ASP.NET pages very fast it still rely on the browser to download and render the output. When adding client-side caching you get an enormous performance benefit when the page is visited more than once by the same browser.

Firefox and IE6/7 has a slightly different way of interpret client-side caching, so you have to implement it correct to get it to work in both browsers. Here is an example of a method that caches the page on the client based on a date. The date should be the exact date of the last time the content changed.

private void SetClientCaching(DateTime lastModified)
{
   Response.Cache.SetETag(lastModified.Ticks.ToString());
   Response.Cache.SetLastModified(lastModified);
   Response.Cache.SetCacheability(HttpCacheability.Public);
   Response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));
   Response.Cache.SetSlidingExpiration(true);
}

If you use a HTTP Handler (.ashx) or some other means to serve files – it could be XML-files – it could be a good idea to do a server-side caching as well. Here’s a method you can call that does just that:

private void SetFileCaching(string fileName)
{
   Response.AddFileDependency(fileName);
   Response.Cache.SetETagFromFileDependencies();
   Response.Cache.SetLastModifiedFromFileDependencies();
   Response.Cache.SetCacheability(HttpCacheability.Public);
   Response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));
   Response.Cache.SetSlidingExpiration(true);
}

You could use the same method to do standard output caching including client-side caching.

SetFileCaching(Request.PhysicalPath);

That makes it more powerful than the regular output cache, because it provides client-side caching as well. As mentioned before, there are a lot of ways to use the different kinds of caching and they all bring something to the plate. The code in this example is something I use all the time because it works great for the projects I work on. In the end it is a personal choice.

Tag:

Add to Del.icio.us | Digg | Yahoo! My Web | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

ASP.NET: Server-side and Client-side Caching
Comments Off
About Mads Kristensen
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/ WebProNews Writer
Top Rated White Papers and Resources

Comments are closed.