C# ASP.NET:当没有 HttpContext.Current 可用(为 null)时如何访问缓存?

发布于 2024-10-07 09:01:30 字数 427 浏览 6 评论 0原文

在 Global.aspx 中的 Application_End() 期间,HttpContext.Current 为 null。我仍然希望能够访问缓存 - 它在内存中,所以想看看我是否可以以某种方式引用它以将位保存到磁盘。

问题 - 当 HttpContext.Current 为 null 时,有没有办法以某种方式引用内存中的缓存?

也许我可以创建一个全局静态变量来存储指向缓存的指针,我可以在 HTTP 请求上更新该指针(伪:"static" = HttpRequest.Current)并检索对缓存的引用通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窗影残 2024-10-14 09:01:30

您应该能够通过 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.

殊姿 2024-10-14 09:01:30

我正在使用以下 getter 返回一个对我有用的 System.Web.Caching.Cache 对象。

get
{
    return (System.Web.HttpContext.Current == null) 
        ? System.Web.HttpRuntime.Cache 
        : System.Web.HttpContext.Current.Cache;
}

这基本上支持了 James Gaunt,但当然只会帮助在应用程序端获取缓存。

编辑:我可能是从詹姆斯链接的斯科特·汉塞尔曼博客上的评论之一中得到这一点的!

I'm using the following getter to return a System.Web.Caching.Cache object which is working for me.

get
{
    return (System.Web.HttpContext.Current == null) 
        ? System.Web.HttpRuntime.Cache 
        : System.Web.HttpContext.Current.Cache;
}

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!

幽蝶幻影 2024-10-14 09:01:30

在 Application_End 事件内,所有缓存对象都已被释放。
如果您想在缓存对象被释放之前访问它,您需要使用类似这样的方式将对象添加到缓存:

将命名空间 System.Web.Caching 导入到您正在使用添加对象到缓存的应用程序。

//Add callback method to delegate
var onRemove = new CacheItemRemovedCallback(RemovedCallback);

//Insert object to cache
HttpContext.Current.Cache.Insert("YourKey", YourValue, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, onRemove);

当要处置该对象时,将调用以下方法:

private void RemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
    //Use your logic here

    //After this method cache object will be disposed
}

如果此方法不适合您,请告诉我。
希望它能帮助您解决您的问题。

最好的问候,迪玛。

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.

//Add callback method to delegate
var onRemove = new CacheItemRemovedCallback(RemovedCallback);

//Insert object to cache
HttpContext.Current.Cache.Insert("YourKey", YourValue, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, onRemove);

And when this object is going to be disposed will be called following method:

private void RemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
    //Use your logic here

    //After this method cache object will be disposed
}

Please let me know if this approach is not right for you.
Hope it will help you with your question.

Best regards, Dima.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文