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?
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.
There is no difference. The getter for Page. Session returns the context session.
The HttpContext is NOT thread safe, accessing it from multiple threads can result in exceptions, data corruption and generally unpredictable results.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With