24 November 2005 09:54
, .NET 2.0
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:
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!