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

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

问题描述

我有一个 C# 网页,我在其中使用 HttpContext.Current.Cache 在服务器缓存中存储了一个 List<> 对象.对象在第一页加载后保存在缓存中.但是,当我刷新页面时,缓存对象为空.有什么想法吗?

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.

一个 IIS 应用程序池因长时间空闲、web.config/binary 更改等而重新启动,也会擦除您的缓存.检查 这篇其他 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,生成数据需要 2m,则您的刷新间隔必须为 28m 或更少,以避免任何用户命中空缓存.

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天全站免登陆