页面刷新后HttpContext.Current.Cache项目空 [英] HttpContext.Current.Cache item null after page refresh

查看:482
本文介绍了页面刷新后HttpContext.Current.Cache项目空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#网页中,我存储一个List<>对象服务器缓存,使用HttpContext.Current.Cache。对象的第一页负荷后保存在缓存中。当我刷新页面,虽然,缓存对象为null。有什么想法?

I've a C# web page in which I'm storing a List<> object in the server cache, using HttpContext.Current.Cache. The object is saved in the cache after the first page load. When I refresh the page though, the cache object is null. Any thoughts?

另外,我想成立一​​个工作来重建对象每次30分钟。我想来提供缓存的版本,直到创建新的,然后用新的取代旧的。 ?
我如何做到这一点。

Also, I'd like to set up a "job" to recreate the object every 30 minutes. I'd like to serve up the cached version until the new one is created, and then replace the old with the new. How do I do that?

在我的Global.asax中的Application_Start,我有以下几点:

In my Global.asax, in Application_Start, I've got the following:

HttpRuntime.Cache.Insert("MainADList", Uf.GetUsers(), null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30));

当我需要它,我执行以下操作:

When I need it, I do the following:

 MainADList = (Users)HttpRuntime.Cache["MainADList"];



这是大部分空。并非总是如此,但几乎总是

Which is for the most part null. Not always, but almost always.

推荐答案

即使您使用填充缓存 Cache.NoAbsoluteExpiration + Cache.NoSlidingExpiration ,ASP.NET可以从缓存中删除项目(即:当免费的系统内存不足)。

Even if you populate your cache using Cache.NoAbsoluteExpiration + Cache.NoSlidingExpiration, ASP.NET can remove items from the cache (i.e.: when the free system memory is low).

CacheItemPriority.NotRemovable 来Cache.Insert()来防止这种情况发生。 查阅MSDN上CachéItemPriority。

Pass CacheItemPriority.NotRemovable to Cache.Insert() to prevent that from happening. Lookup CachéItemPriority on MSDN.

长的空闲时间,web.config文件/二元变化等IIS应用程序池重启也会擦缓存。检查这个其他SO后,HttpRuntime.Cache.Insert()不拿着缓存值

An IIS application pool restart by long idle time, web.config/binary change, etc. will also wipe your cache. Check this other SO post, HttpRuntime.Cache.Insert() Not holding cached value

关于创建工作,刷新缓存;我不认为有经验的最佳缓存策略的规则。这将在很大程度上取决于用户如何与你的网页,多长时间,怎么并发,以及专门,有多少时间没有考虑到产生时,高速缓存未命中的数据。

About creating a job to refresh the cache; I don't think there is a rule of thumb for the best Cache strategy. It will heavily depend on how your users interact with your web page, how often, how concurrent, and specially, how much time does it take to generate that data when the cache is not hit.

如果需要生成数据为任何用户无法接受的时候,你可以设置刷新缓存的工作,但它的间隔应小于缓存TTL +时间的总和的生成/检索需要。
为例,如果你的缓存为30m,它需要2μm至生成数据,你的刷新间隔必须是28米以下,以避免任何用户打一个空的高速缓存。

If the time it takes to generate the data is unacceptable for any user, you can set up a job that refreshes the cache, but it's interval should be less than the sum of the cache TTL + time that the generation/retrieval takes. For example, if your cache is 30m and it takes 2m to generate the data, your refresh interval must be 28m or less, to avoid any user to hit an empty cache.

这篇关于页面刷新后HttpContext.Current.Cache项目空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆