Caching in Windows Applications

Published 24 November 2005 09:54 by Derek Lakin
Filed Under: , ,

I've often heard that you can use the ASP.NET caching features provided by the .NET Framework in Windows applications, but never tried it. Well, today I read a post by the great Scott Hanselman that confirms it:

 I also started a conversation on a list server and here's what came of that:

Rob Howard said:

Yes, it's fairly common (and easy) to do. You just have to include a reference to the System.Web assembly in non-web applications; which may have led to your "creep out" – for what it’s worth it used to do the same to me :) 
FWIW, I believe (from memory) the recommended way you grab a reference outside of a web application is:
using System.Web;
using System.Web.Caching;

Cache cache = HttpRuntime.Cache;
<snip>...the Cache is just too important of a feature to only belong to ASP.NET.

It seems that the biggest obstacle most people come across is a psychological one of actually adding a reference to System.Web and System.Web.Caching! Once you can get over that then you have access to the great lightweight caching features, including the cache expiration stuff.

One commenter did suggest that you could always use the Enterprise Library Caching Block from the Microsoft patterns & practices website, but I would tend to agree with Scott: there's just too much going on with the caching block and probably a bit too heavyweight for a lot of applications.

Now I can't wait to try this out in my next Windows application!


# re: Caching in Windows Applications @ 24 November 2005 10:44

I've used HttpRuntime.Cache a few times in projects I've worked on. Saves me having to whip up my own solution around Hashtable :)

Neil Cowburn