Caching In Non-web Applications, A New Perspective

Output caching enables one to store the generated output of pages, controls, and HTTP responses in memory. Due to it’s power and awesomeness,  it had tempted developers to use the  namespace into their projects and in other applications such as Windows Forms and WPF apps.

In the .NET Framework 3.5 and earlier versions, ASP.NET provided an in-memory cache implementation in the System.Web.Caching namespace. In these previous versions of the .NET Framework, caching was available only in the System.Web namespace and therefore required a dependency on ASP.NET classes.

So now the question arises, that can we implement caching without referencing to System.Web.dll ?

The answer is, Yes we can.

To make caching available for all applications, the .NET Framework 4 introduces a new assembly,  System.Runtime.Caching.dll, this assembly contains a new caching API in the System.Runtime.Caching namespace. The classes in this namespace provide a way to use caching facilities like those in ASP.NET, but without a dependency on the System.Web assembly.

In order to add a object to the cache, do so

ObjectCache cache = MemoryCache.Default;

Cache.Add("Key", objectToCache, DateTime.Now.AddDays(30));


In  order to fetch it, do so

Object ob = Cache[“key”];
150 150 Burnignorance | Where Minds Meet And Sparks Fly!