Is the HttpContext.Current.Cache available to all sessions

前端 未结 2 1601
一整个雨季
一整个雨季 2021-02-07 05:06

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 a

2条回答
  •  心在旅途
    2021-02-07 05:39

    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.

提交回复
热议问题