我正在使用 .NET 4.0 MemoryCache
类,我想以线程安全的方式添加或替换缓存中的现有项,但我还想知道我是否替换了现有项或添加了新项。
据我所知,Set
方法旨在原子地替换缓存中的项目(如果存在),而 AddOrGetExisting
方法将自动为我获取现有项目而不替换它。
但是,Set 方法不会返回被替换的项目,它只是替换它。可以将已删除的事件连接到 CacheItemPolicy
。对于项目,但很难将其与替换它的新项目相匹配。
我可以用下面的方法来近似它,但它看起来有点重(因为要求缓存基本上添加一个项目两次)我想问问是否有人知道更简单的方法。
public object SetAndGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
{
lock (_cacheLock)
{
var existing = _cache.AddOrGetExisting(key, value, policy, regionName);
if (existing != null)
{
_cache.Set(key, value, policy, regionName);
return existing;
}
return null;
}
}
最佳答案
在考虑之后,在您的示例中,无论如何您都在更新它,所以 AddOrGetExisting
不是最好的,正如您提到的,无论如何您稍后都会设置它。下面是一个简单的实现,我认为符合您的要求。如果我错了,请告诉我!
public class MemoryCacheWithEvents
{
private static MemoryCache _cache = new MemoryCache("myCache");
private static object _syncLock = new object();
public EventHandler<CacheChangeEventArgs> AddingCacheItem;
public EventHandler<CacheChangeEventArgs> UpdatingCacheItem;
public object GetAndSetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
{
lock (_syncLock)
{
var cacheItem = new CacheItem(key, value, regionName);
var existing = _cache.GetCacheItem(key, null);
if (existing == null)
{
OnAddingCacheItem(new CacheChangeEventArgs(null, cacheItem));
}
else
{
OnUpdatingCacheItem(new CacheChangeEventArgs(existing, cacheItem));
}
_cache.Set(cacheItem, policy);
return existing;
}
}
public virtual void OnAddingCacheItem(CacheChangeEventArgs eventArgs){
var handler = AddingCacheItem;
if (handler != null)
{
handler(this, eventArgs);
}
}
public virtual void OnUpdatingCacheItem(CacheChangeEventArgs eventArgs){
var handler = UpdatingCacheItem;
if (handler != null)
{
handler(this, eventArgs);
}
}
}
public class CacheChangeEventArgs : EventArgs
{
public object OldCacheItem { get; set; }
public object NewCacheItem { get; set; }
public CacheChangeEventArgs(object oldCacheItem, object newCacheItem)
{
this.OldCacheItem = oldCacheItem;
this.NewCacheItem = newCacheItem;
}
}
关于c# - MemoryCache.Set 返回删除的缓存项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16018009/