Disposing and then creating a new cache is problematic at times. Having a Clear() method resolves that.
I think adding ICacheProvider.Clear() is the best way to do this. But here is code that works:
public static void ExClear(this IAppCache service)
{
IMemoryCache? provider = service.CacheProvider.GetPrivateFieldValue<IMemoryCache>("cache");
if (provider is MemoryCache memoryCache)
memoryCache.Clear();
else Trap.Break(); // need to find a solution for this!!!
}
Disposing and then creating a new cache is problematic at times. Having a
Clear()
method resolves that.I think adding
ICacheProvider.Clear()
is the best way to do this. But here is code that works: