【发布时间】:2020-10-11 20:23:06
【问题描述】:
我在我的一个项目中使用MemoryCache 来缓存一些键和值。
private bool CacheEntries<T>(MemoryCache memoryCache, string cacheKey, T value, Configuration config, Action<MemoryCacheEntryOptions> otherOptions = null)
{
int minutes = randomGenerator.Next(config.LowTime, config.HighTime);
MemoryCacheEntryOptions options = new MemoryCacheEntryOptions()
{
Size = config.Size,
Priority = config.Priority,
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(minutes)
};
if (otherOptions != null) otherOptions(options);
CacheKeys.Add(cacheKey);
memoryCache.Set<T>(cacheKey, value, options);
return true;
}
如上所示,我的密钥已过期。此外,我将相同的密钥存储在 CacheKeys HashSet 数据结构中以供以后使用。有什么方法可以在我的MemoryCache 上附加一个侦听器,只要它使任何项目到期,就应该调用该侦听器,以便我也可以从CacheKeysHashSet 中删除这些键。基本上我想与CacheKeys 和MemoryCache 中的内容保持一致。
这可能吗?
【问题讨论】:
标签: c# memorycache