Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the HttpContext.Current.Cache available to all sessions

Tags:

As per title. I want to be able to save some data in a cache object but this object must be available to all users/sessions and can expire.

What is the best method to achieve this in a asp.net web app?

like image 594
Ekk Avatar asked May 04 '10 02:05

Ekk


People also ask

What is use of HttpContext current session?

The Session property provides programmatic access to the properties and methods of the HttpSessionState class. In order to use session state you have to enable it. For information about how to enable session state, see Configuring Session State in ASP.NET Session State Overview.

What is the difference between session and HttpContext current session?

There is no difference. The getter for Page. Session returns the context session.

Is HttpContext current Cache thread safe?

The HttpContext is NOT thread safe, accessing it from multiple threads can result in exceptions, data corruption and generally unpredictable results.

What is HttpContext cache?

HttpContext. Current. Cache is a class that provides caching of any kind of serializable objects. It in itself equates to HttpRuntime. Cache to muddy your waters even more.


1 Answers

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., you'll end up with a NullReferenceException.

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

like image 198
Aaronaught Avatar answered Oct 13 '22 10:10

Aaronaught