Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Caching tip

Ron Jacobs showed this one in his Caching presentation. The idea is that you can use the ASP.NET cache from any application, you don't need to be inside a Web app:

System.Web.HttpRuntime r = new System.Web.HttpRuntime();

System.Web.HttpRuntime.Cache myCache = System.Web.HttpRuntime.Cache;

myCache .Add("Key1", "Value 1",null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, null);

I did not know you could do this.

5 Comments

  • Cool!!! I've been looking for a way to unit test middle tier stuff that would be using the ASP .NET cache. This is just the tip I needed. Looking forward to trying this out!





    Cheers,


    Scott

  • Great tip, never knew you could do that!

  • Of course, you will still have a dependecy on the ASP.NET class libraries which may not be desirable.

  • Just to add to Alex's comment, the ASP.NET classes required to do this are not installed in many versions of the framework. For instance, if your app targets 98, Me, XP Home, or CE devices, then I think you will not be able to pull this off.

  • Ed, the cache is for caching, not for keeping state. The session is for keeping state. You could want to use a cache in your middle tier (i.e., a .NET remoting application), or in your Windows forms app (i.e., you want to keep DataSets in the client). In this second case, the comments Alex and Wilson made make sense, as you cannot rely on the ASP.NET libraries.





    I am not saying that this is a good idea, I just found it interesting ;)

Comments have been disabled for this content.