ead*_*dam 21 c# asp.net-core-mvc asp.net-core
我可以找到一种remove方法来IMemoryCache从其键中删除对象.有没有办法重置整个缓存并删除所有对象?
编辑:
如何清除MemoryCache?
链接中提供的Dispose方法在asp.net 5中给出了一个例外.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Microsoft.Extensions.Caching.Memory.MemoryCache'.
ale*_*eha 28
https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory 部分缓存依赖项
使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
最简单的方法是Compact(1.0)如果它可用。此代码将使用扩展方法清除内存缓存(在单元测试和 .NET 核心 2.2 和 3.1 上的生产中测试)。如果Compact不可用,则使用回退方法,从公共Clear方法开始,然后是内部Clear方法。如果这些都不可用,则抛出异常。
/// <summary>
/// Clear IMemoryCache
/// </summary>
/// <param name="cache">Cache</param>
/// <exception cref="InvalidOperationException">Unable to clear memory cache</exception>
/// <exception cref="ArgumentNullException">Cache is null</exception>
public static void Clear(this IMemoryCache cache)
{
if (cache == null)
{
throw new ArgumentNullException("Memory cache must not be null");
}
else if (cache is MemoryCache memCache)
{
memCache.Compact(1.0);
return;
}
else
{
MethodInfo clearMethod = cache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public);
if (clearMethod != null)
{
clearMethod.Invoke(cache, null);
return;
}
else
{
PropertyInfo prop = cache.GetType().GetProperty("EntriesCollection", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public);
if (prop != null)
{
object innerCache = prop.GetValue(cache);
if (innerCache != null)
{
clearMethod = innerCache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public);
if (clearMethod != null)
{
clearMethod.Invoke(innerCache, null);
return;
}
}
}
}
}
throw new InvalidOperationException("Unable to clear memory cache instance of type " + cache.GetType().FullName);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
为了重置 IMemoryCache 对象,我创建了一个使用 GetType() 中的 GetProperty() 访问 EntriesCollection 属性的方法。有了集合,我使用 GetType() 的 GetMethod() 来调用 Clear() 方法。
保持这样的状态:
public void IMemoryCacheClear(IMemoryCache memoryCache)
{
PropertyInfo prop = memoryCache.GetType().GetProperty("EntriesCollection", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public);
if (prop is null)
return;
object innerCache = prop.GetValue(memoryCache);
MethodInfo clearMethod = innerCache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public);
clearMethod.Invoke(innerCache, null);
}
Run Code Online (Sandbox Code Playgroud)
目前我迁移到.NET 7,我需要更正该方法,因为 EntriesCollection 不再返回集合数据。从 .NET 7 开始,Clear() 方法现在出现在 MemoryCache 类中。因此,要修复此问题,只需将变量转换为 MemoryCache 并调用 Clear() 方法即可。
修正是这样的:
public void IMemoryCacheClear(IMemoryCache memoryCache)
{
if (memoryCache is MemoryCache cache)
{
cache.Clear();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
如果您使用标准 MemoryCache,此代码会有所帮助。文档:https : //docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-3.1#memorycachecompact
_cache.Compact(1.0);
Run Code Online (Sandbox Code Playgroud)
截至 RC1 的答案是,根据我所读到的和被告知的,你不能开箱即用(我确实在 GitHub 上读到,也许有一种方法可以创建触发器来促进即将到来的这一点)。
目前,您可以获取、设置和删除。我认为你的选择是:
https://github.com/blakepell/Caching/commit/165ae5ec13cc51c44a6007d6b88bd9c567e1d724
我昨晚发布了这个问题,试图弄清楚是否有一个好的方法来专门检查缓存中的内容(询问为什么我们没有办法)。如果你不问,你永远不会知道这是否重要,所以我想为什么不呢。
https://github.com/aspnet/Caching/issues/149
| 归档时间: |
|
| 查看次数: |
11030 次 |
| 最近记录: |