【发布时间】:2019-12-20 17:25:07
【问题描述】:
.Net 核心内存缓存。我希望在从缓存中删除项目时收到通知。实施此示例后: https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-3.1
=> CreateCallbackEntry()。当我在
中放置断点时驱逐回调:
private static void EvictionCallback(object key, object value,
EvictionReason reason, object state)
{
var message = $"Entry was evicted. Reason: {reason}.";
// i should reach this after 10 seconds
}
只有在尝试再次读取刚刚存储在缓存中的相同键时才会触发断点。否则,即使网页保持打开状态,也不会调用该驱逐方法。
控制器:
public void OnGet()
{
_cache.GetOrCreate("key", item);
// eviction callback does not fire until I query for the same key again below
var t = _cache.GetOrCreate("key", item)
}
缓存:
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(10))
.RegisterPostEvictionCallback(callback: EvictionCallback, state: this);
cacheEntry = await createItem();
_cache.Set(key, cacheEntry, cacheEntryOptions);
【问题讨论】:
-
您只是希望在超时后执行回调吗?这不是它的工作原理,您需要查询缓存中的项目,然后它会调用它。
-
嗯。我知道了。有没有办法以某种方式修改它以实现这一目标?
-
这行得通!。非常感谢
-
我猜这是重复的,我会关闭它:)
标签: c# .net memorycache