.Net core memory cache. I would like to be notified when an item is removed from cache. Having implemented this sample: https://learn.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-3.1
=> CreateCallbackEntry(). When I put a breakpoint within
Eviction callback:
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
}
the breakpoint is ONLY hit when trying read that same key again that was just stored in cache. Otherwise that eviction method never gets called even though the web page stays open.
Controller:
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)
}
Cache:
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(10))
.RegisterPostEvictionCallback(callback: EvictionCallback, state: this);
cacheEntry = await createItem();
_cache.Set(key, cacheEntry, cacheEntryOptions);