Repository navigation
Description
Consider the following:
_cache.Get<CancellationTokenSource>(CacheKeys.DependentCTS).Cancel();
Here, we're manually acquiring a cached CancellationTokenSource and calling Cancel. This will invalidate the cache for anyone using that token, eg people who have done this:
var cts = new CancellationTokenSource();
_cache.Set(CacheKeys.DependentCTS, cts);
using (var entry = _cache.CreateEntry(CacheKeys.Parent))
{
// expire this entry if the dependant entry expires.
entry.Value = DateTime.Now;
entry.RegisterPostEvictionCallback(DependentEvictionCallback, this);
_cache.Set(CacheKeys.Child,
DateTime.Now,
new CancellationChangeToken(cts.Token));
}
It's not clear from the docs whether, when the cached CancellationTokenSource is expired from the cache, whether that has the effect of triggering all the dependant cached entries to be invalidated too. For instance, if I'm only caching my CancellationTokenSource for 5 minutes, I'd love when it's being evacuated from the cache for all the dependents to go with it. But I don't know whether cache evacuation will trigger the .Cancel() on the token. It's certainly my hope that is the behaviour but I'd love the docs to be clear on that.
I suspect that isn't what happens as it's a bit "magical"
At the moment, the way I read it, it suggests that only calling _cache.Get<CancellationTokenSource>(CacheKeys.DependentCTS).Cancel(); will trigger the invalidation; ie it has to be explicitly triggered. If that is the case, then guidance on how to do that would improve these docs I think. My guess would be that this would be this would be done using RegisterPostEvictionCallback if that's the case?
PS great work on the docs generally - they're really good!
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 13f5f8f9-18dd-9c12-9ed1-fcc6b44ba7fd
- Version Independent ID: 640b4f36-5886-7b69-1c95-cfc8574a02fd
- Content: Cache in-memory in ASP.NET Core
- Content Source: aspnetcore/performance/caching/memory.md
- Product: aspnet-core
- Technology: aspnetcore-performance
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
Activity
[-]Are[/-][+]Are CancellationTokenSources triggered when invalidated from cache? Hope so![/+]mkArtakMSFT commentedon Jun 29, 2020
@ericstj who would be best person to handle this?
ericstj commentedon Jun 30, 2020
Per https://github.com/dotnet/runtime/blob/master/docs/area-owners.md @maryamariyan @eerhardt
Though I'm not sure when we'll get to this as we're still ramping up.