简体   繁体   English

页面刷新后HttpContext.Current.Cache项为null

[英]HttpContext.Current.Cache item null after page refresh

I've a C# web page in which I'm storing a List<> object in the server cache, using HttpContext.Current.Cache. 我有一个C#网页,我在其中使用HttpContext.Current.Cache在服务器缓存中存储List <>对象。 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? 有什么想法吗?

Also, I'd like to set up a "job" to recreate the object every 30 minutes. 另外,我想设置一个“工作”来每30分钟重新创建一个对象。 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? 我怎么做?

In my Global.asax, in Application_Start, I've got the following: 在我的Global.asax中,在Application_Start中,我有以下内容:

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. 其中大部分是null。 Not always, but almost always. 不总是,但几乎总是。

Even if you populate your cache using Cache.NoAbsoluteExpiration + Cache.NoSlidingExpiration , ASP.NET can remove items from the cache (ie: when the free system memory is low). 即使使用Cache.NoAbsoluteExpiration + Cache.NoSlidingExpiration填充缓存,ASP.NET也可以从缓存中删除项目(即:当空闲系统内存不足时)。

Pass CacheItemPriority.NotRemovable to Cache.Insert() to prevent that from happening. CacheItemPriority.NotRemovable传递给Cache.Insert()以防止发生这种情况。 Lookup CachéItemPriority on MSDN. 在MSDN上查找CachéItemPriority。

An IIS application pool restart by long idle time, web.config/binary change, etc. will also wipe your cache. 通过长时间空闲,web.config /二进制更改等重新启动IIS应用程序池也将擦除您的缓存。 Check this other SO post, HttpRuntime.Cache.Insert() Not holding cached value 检查其他SO帖子,HttpRuntime.Cache.Insert()不保存缓存值

About creating a job to refresh the cache; 关于创建作业以刷新缓存; I don't think there is a rule of thumb for the best Cache strategy. 我认为最好的Cache策略没有经验法则。 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. 它将在很大程度上取决于您的用户与您的网页的交互方式,频率,并发时间,以及特别是在未达到缓存时生成数据所需的时间。

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. 如果生成数据所花费的时间对于任何用户都是不可接受的,则可以设置刷新缓存的作业,但其间隔应小于生成/检索所采用的缓存TTL +时间的总和。 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. 例如,如果您的缓存为30米并且生成数据需要2米,则刷新间隔必须为28米或更短,以避免任何用户点击空缓存。

Can you use the System.Runtime.Caching instead. 你可以使用System.Runtime.Caching That way you're not dependent on the HttpRuntime namespace. 这样你就不依赖于HttpRuntime命名空间了。

I suspect you're getting null because .Net is clearing it due to web-server restarts. 我怀疑你是因为网络服务器重启而.Net正在清除它。 Would it be better to do something along the lines of 做一些事情会更好吗?

    public List<User> GetUsers()
    {
        var cache = System.Runtime.Caching.MemoryCache.Default;
        if (cache["MainADList"] == null)
        {
            // Rebuild cache. Perhaps for Multithreading, you can do object locking
            var cachePolicy = new System.Runtime.Caching.CacheItemPolicy() { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(30) };
            var users = Uf.GetUsers();
            cache.Add(new System.Runtime.Caching.CacheItem("MainADList", users), cachePolicy);

            return users;
        }

        return cache["MainADList"] as List<User>; 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM