我可以找到一个remove
方法,通过它的键从IMemoryCache
中删除一个对象。有没有办法重置整个缓存并删除所有对象?
编辑:
How to clear MemoryCache?链接中提供的Dispose方法在asp.net 5. ObjectDisposedException: Cannot access a disposed object.
Object name: 'Microsoft.Extensions.Caching.Memory.MemoryCache'.
中给了我一个例外
https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory Section Cache依赖项
使用CancellationTokenSource允许将多个缓存条目作为一个组逐出
这段代码对我有用:
public class CacheProvider
{
private static CancellationTokenSource _resetCacheToken = new CancellationTokenSource();
private readonly IMemoryCache _innerCache;
/* other methods and constructor removed for brevity */
public T Set<T>(object key, T value)
{
/* some other code removed for brevity */
var options = new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.Normal).SetAbsoluteExpiration(typeExpiration);
options.AddExpirationToken(new CancellationChangeToken(_resetCacheToken.Token));
_innerCache.Set(CreateKey(type, key), value, options);
return value;
}
public void Reset()
{
if (_resetCacheToken != null && !_resetCacheToken.IsCancellationRequested && _resetCacheToken.Token.CanBeCanceled)
{
_resetCacheToken.Cancel();
_resetCacheToken.Dispose();
}
_resetCacheToken = new CancellationTokenSource();
}
}
RC1的答案是,你不能从我读过的内容中做到这一点并且被告知(我在GitHub上读过,可能有一种方法可以创建触发器来促进这种情况的发生)。
目前,您将获得Get,Set和Remove。我看到你的选择:
https://github.com/blakepell/Caching/commit/165ae5ec13cc51c44a6007d6b88bd9c567e1d724
我昨晚发布了这个问题,试图弄清楚是否有一个很好的方法来专门检查缓存中的内容(问为什么我们没有办法)。如果你不问你永远不会知道它是否重要,所以我想为什么不。
有很多黑客可以做到这一点。如果你有正确的IMemoryCache,你可以使用Compact(1.0)
,否则这个hack有效:
此代码将起作用(在单元测试和.NET核心2.2上的生产中测试):
PropertyInfo prop = cache.GetType().GetProperty("EntriesCollection", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public);
object innerCache = prop.GetValue(cache);
MethodInfo clearMethod = innerCache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public);
clearMethod.Invoke(innerCache, null);
我的解决方案是将缓存中所有项目的新到期日期设置为1毫秒。然后他们过期,因此缓存刷新。
我的解决方案是创建一个包装器,重新公开现有的几个方法,并通过用一个全新的方法替换MemoryCache对象来添加一个缺少的方法。工作对我来说很好。代码如下:
public interface IMyMemoryCache : IMemoryCache
{
void Reset();
}
public class MyMemoryCache: IMyMemoryCache
{
IMemoryCache _memoryCache;
public MyMemoryCache()
{
Reset();
}
public void Dispose()
{
_memoryCache.Dispose();
}
public bool TryGetValue(object key, out object value)
{
return _memoryCache.TryGetValue(key, out value);
}
public ICacheEntry CreateEntry(object key)
{
return _memoryCache.CreateEntry(key);
}
public void Remove(object key)
{
_memoryCache.Remove(key);
}
public void Reset()
{
_memoryCache = new MemoryCache(new MemoryCacheOptions());
}
}