HttpRuntime.Cache 不会过期



我对以下代码有问题。我有以下代码,

            if (HttpRuntime.Cache[cacheKey] == null)
            {
                AddTask(cacheKey, JsonConvert.SerializeObject(result), 60 * 30);
            }
            r = JsonConvert.DeserializeObject<Result>(HttpRuntime.Cache[cacheKey].ToString());
            HttpRuntime.Cache[cacheKey] = JsonConvert.SerializeObject(r); 

           private static void AddTask(string key, string value, int seconds)
            {
                HttpRuntime.Cache.Insert(key, value, null,
                    DateTime.Now.AddSeconds(10), Cache.NoSlidingExpiration,
                    CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemoved));
            }
    public static void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
    {
    }

我正在做的只是添加和更新(如果存在)缓存。然后在 10 秒后我想检查一些东西。但是我的CacheItemRemoved回调从未调用过。

何时使用

HttpRuntime.Cache[cacheKey] = JsonConvert.SerializeObject(r);

然后缓存时间重置为默认时间。现在我不再使用字符串,而是使用对象实例而不更新缓存。

notification = HttpRuntime.Cache[cacheKey] as Notification;
HttpRuntime.Cache.Insert(key, notificationResult , null,
                DateTime.Now.AddSeconds(10), Cache.NoSlidingExpiration,
                CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(CacheItemRemoved));

最新更新