C# ASP.NET:当没有 HttpContext.Current 可用(为 null)时如何访问缓存?
在 Global.aspx 中的 Application_End()
期间,HttpContext.Current 为 null。我仍然希望能够访问缓存 - 它在内存中,所以想看看我是否可以以某种方式引用它以将位保存到磁盘。
问题 - 当 HttpContext.Current 为 null 时,有没有办法以某种方式引用内存中的缓存?
也许我可以创建一个全局静态变量来存储指向缓存的指针,我可以在 HTTP 请求上更新该指针(伪:"static
)并检索对缓存的引用通过 Application_End() 中的指针?
在没有Http请求的情况下,有没有更好的方法来访问内存中的Cache?
During Application_End()
in Global.aspx, HttpContext.Current is null. I still want to be able to access cache - it's in memory, so want to see if I can reference it somehow to save bits to disk.
Question - is there a way to reference cache in memory somehow when HttpContext.Current is null?
Perhaps I could create a global static variable that would store pointer to cache that I could update on HTTP requests (pseudo: "static <pointer X>" = HttpRequest.Current
) and retrieve a reference to cache through that pointer in Application_End()?
Is there a better way to access Cache in memory when there is no Http Request is made?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够通过 HttpRuntime.Cache
http://www.hanselman.com/blog/ 访问它根据 Scott 的说法, UsingTheASPNETCacheOutsideOfASPNET.aspx
- 查看 Reflector HttpContext.Current.Cache 只是调用 HttpRuntime.Cache - 因此您最好始终以这种方式访问它。
You should be able to access it via HttpRuntime.Cache
http://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx
According to Scott - looking at Reflector HttpContext.Current.Cache just calls HttpRuntime.Cache - so you might as well always access it this way.
我正在使用以下 getter 返回一个对我有用的 System.Web.Caching.Cache 对象。
这基本上支持了 James Gaunt,但当然只会帮助在应用程序端获取缓存。
编辑:我可能是从詹姆斯链接的斯科特·汉塞尔曼博客上的评论之一中得到这一点的!
I'm using the following getter to return a System.Web.Caching.Cache object which is working for me.
Which basically backs up James Gaunt but of course is only going to help get the cache in Application End.
Edit: I probably got this from one of the comments on the Scott Hanselman blog James linked to!
在 Application_End 事件内,所有缓存对象都已被释放。
如果您想在缓存对象被释放之前访问它,您需要使用类似这样的方式将对象添加到缓存:
将命名空间 System.Web.Caching 导入到您正在使用添加对象到缓存的应用程序。
当要处置该对象时,将调用以下方法:
如果此方法不适合您,请告诉我。
希望它能帮助您解决您的问题。
最好的问候,迪玛。
Inside Application_End event all cache objects are already disposed.
If you want to get access to cache object before it will be disposed, you need to use somethink like this to add object to cache:
Import namespace System.Web.Caching to your application where you are using adding objects to cache.
And when this object is going to be disposed will be called following method:
Please let me know if this approach is not right for you.
Hope it will help you with your question.
Best regards, Dima.