C# HttpRuntime.Cache未过期?
我对以下代码有问题。我有以下代码C# HttpRuntime.Cache未过期?,c#,asp.net,caching,C#,Asp.net,Caching,我对以下代码有问题。我有以下代码 if (HttpRuntime.Cache[cacheKey] == null) { AddTask(cacheKey, JsonConvert.SerializeObject(result), 60 * 30); } r = JsonConvert.DeserializeObject<Result>(HttpRuntime
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)
{
}
if(HttpRuntime.Cache[cacheKey]==null)
{
AddTask(cacheKey,JsonConvert.SerializeObject(结果),60*30);
}
r=JsonConvert.DeserializeObject(HttpRuntime.Cache[cacheKey].ToString());
HttpRuntime.Cache[cacheKey]=JsonConvert.serializer对象;
私有静态void AddTask(字符串键、字符串值、整数秒)
{
HttpRuntime.Cache.Insert(key,value,null,
DateTime.Now.AddSeconds(10),Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable,新的CacheItemRemovedCallback(CacheItemRemoved));
}
公共静态void CacheItemRemoved(字符串k、对象v、CacheItemRemovedR)
{
}
我所做的只是添加和更新(如果存在)缓存。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));
无法保证该项将被删除-这不是缓存的重点。您试图在此处执行什么操作?在缓存中添加和更新项。经过一段时间后,我想删除缓存并在回调中保存所有缓存数据。好的,
cache
对此不起作用。首先,它不能保证该项被删除——其次,它不能保证当该项被删除时,回调被调用并完成执行。它是一个缓存,而不是一个写缓冲区-如果你需要一个写缓冲区,你需要自己制作一个。