gpt4 book ai didi

c# - System.Runtime.Cache 立即过期

转载 作者:太空狗 更新时间:2023-10-29 23:51:48 24 4
gpt4 key购买 nike

我有一个网页将一些查询字符串值缓存 30 秒,这样它就不会收到重复的值。我正在使用以下类(class):

public class MyCache   {

private static ObjectCache cache = MemoryCache.Default;

public MyCache() { }


public void Insert(string key, string value)
{

CacheItemPolicy policy = new CacheItemPolicy();
policy.Priority = CacheItemPriority.Default;
policy.SlidingExpiration = new TimeSpan(0, 0, 30);
policy.RemovedCallback = new CacheEntryRemovedCallback(this.Cacheremovedcallback);

cache.Set(key, value, policy);
}

public bool Exists(string key)
{
return cache.Contains(key);
}

public void Remove(string key)
{
cache.Remove(key);
}

private void Cacheremovedcallback(CacheEntryRemovedArguments arguments)
{
FileLog.LogToFile("Cache item removed. Reason: " + arguments.RemovedReason.ToString() + "; Item: [" + arguments.CacheItem.Key + ", " + arguments.CacheItem.Value.ToString() + "]");
}
}

这几周都运行良好,然后突然缓存不再保留这些值。 CacheRemoved 回调在项目插入缓存后立即触发,我得到删除原因:CacheSpecificEviction这是在 Windows Server 2008 SP1、IIS7.5 和 .NET 4.0 上运行。在此期间没有对操作系统或 IIS 应用任何更改。

有没有办法解决这个问题,如果没有,是否有更好的缓存解决方案可用于网页?

提前谢谢你。

最佳答案

查看 MemoryCacheStore 的源代码(应该是 MemoryCache.Default 使用的默认存储)似乎带有参数的删除回调 CacheSpecificEviction 仅在释放缓存时调用:

https://referencesource.microsoft.com/#System.Runtime.Caching/System/Caching/MemoryCacheStore.cs,230

环顾四周,确保您的缓存对象没有被意外丢弃。

关于c# - System.Runtime.Cache 立即过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14892621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com