开发者

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

How can I access HTTP Cache in a C# class librar开发者_运维知识库y ?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜