是提供给所有会话HttpContext.Current.Cache [英] Is the HttpContext.Current.Cache available to all sessions
问题描述
按标题。我希望能够保存在缓存中的对象的一些数据,但这个对象必须是提供给所有用户/会话,可以到期。
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.
什么是最好的方法,在asp.net web应用程序实现这一目标?
What is the best method to achieve this in a asp.net web app?
推荐答案
HttpContext.Current
适用于所有的页的,但不一定所有线程。如果您尝试使用一个后台线程里面,线程池
委托,异步调用(使用ASP.NET异步页)等等,你会最终有一个的NullReferenceException
。
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
.
如果您需要获得访问来自库类,即类缓存没有当前请求的知识,你应该使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpruntime.cache.aspx\">HttpRuntime.Cache代替。这是更可靠的,因为它不依赖于一个的HttpContext
。
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
.
这篇关于是提供给所有会话HttpContext.Current.Cache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!