asp.net – Is the HttpContext.Current.Cache available to all sessions

asp.net – Is the HttpContext.Current.Cache available to all sessions

HttpContext.Current is available to all pages, but not necessarily to all threads. If you try to use it inside a background thread, ThreadPool delegate, async call (using an ASP.NET Async page), etc., youll end up with a NullReferenceException.

If you need to get access to the cache from library classes, i.e. classes that dont have knowledge of the current request, you should use HttpRuntime.Cache instead. This is more reliable because it doesnt depend on an HttpContext.

HttpContext.Current.Cache will be present, but Current should only be used if you cant get to your context member.

Also to answer your second question, yes, the Cache object is global to the application.

Heres a good intro to caching…

How to cache in ASP.NET by using Visual C# .NET

and…

Caching with ASP.NET . Dont skip part 2, Data Caching

asp.net – Is the HttpContext.Current.Cache available to all sessions

Leave a Reply

Your email address will not be published.