Shyju
Shyju

Reputation: 218762

How can I access HTTP Cache in a C# class library?

How can I access HTTP Cache in a C# class library ?

Upvotes: 17

Views: 14612

Answers (3)

Daniel Schaffer
Daniel Schaffer

Reputation: 57852

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.

Upvotes: 24

GrayWizardx
GrayWizardx

Reputation: 21141

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.

Upvotes: 0

M4N
M4N

Reputation: 96571

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.

Upvotes: 0

Related Questions