3

我正在使用 .NET 4.0MemoryCache类,我想以线程安全的方式添加或替换缓存中的现有项目,但我也想知道我是替换了现有项目还是添加了新项目。

据我所知,该Set方法旨在以原子方式替换缓存中的项目(如果存在),而该AddOrGetExisting方法将以原子方式获取现有项目而不替换它。

但是,Set 方法不会返回被替换的项目,它只是替换它。可以将已删除的事件与 for 项目挂钩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;
    }
}
4

1 回答 1

1

在考虑之后,在您的示例中,无论如何您都在更新它,所以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;
    }
}
于 2013-04-15T16:15:46.867 回答