tags:

views:

158

answers:

3

How can i Access HTTP Cache in a C# class library ?

+1  A: 

If you mean the ASP.NET cache, then you can use System.Web.HttpContext.Current.Cache.

HttpContext.Current can also be used the access the current request, response, etc.

M4N
A: 

You can access the HTTP cache using the System.Web.Caching namespace as detailed in this MSDN article: System.Web.Cache

Once you import the namespace there is a static accessor for the cache which you can reference. As long as the cache has been instantiated prior (by the ASP.NET process, or another initiator) you will have access to the cache, otherwise it will return a NULL reference.

GrayWizardx
+3  A: 

It is recommended that you use System.Web.HttpRuntime.Cache rather than System.Web.HttpContext.Current.Cache, as explained in this article.

Additionally, while the article talks about performance, I've also had issues in the past where HttpContext.Current isn't always available when you'd expect it to be, especially when dealing with asynchronous handlers.

Another thing to note is that if you aren't accessing the cache in the context of an HTTP request, HttpContext won't help you, since there won't be a relevant context for you to access.

Daniel Schaffer