使用ConcurrentDictionary作为缓存并处理更新

时间:2015-01-23 18:24:28

标签: c# .net parallel-processing concurrentdictionary

我有以下代码,现在我想添加UpdateSetting方法。

我可以看到的最好的方法是通过TryUpdate上的ConcurrentDictionary,但这意味着知道以前的值,这样就需要调用GetSetting这似乎有点令人讨厌。你的想法是什么?还有更好的方法吗?

注意:如果该值不在缓存中,则不应执行任何操作。成功更新缓存后,应调用settingRepository.Update

由于

public class MySettings : IMySettings
    {
        private readonly ISettingRepository settingRepository;
        private readonly ConcurrentDictionary<string, object> cachedValues = new ConcurrentDictionary<string, object>();


        public MySettings(ISettingRepository settingRepository)
        {
            this.settingRepository = settingRepository;
        }

        public string GetSetting(string key)
        {
            return this.GetSetting<string>(key);
        }

        public T GetSetting<T>(string key)
        {
            object value;
            if (!this.cachedValues.TryGetValue(key, out value))
            {
                value = this.GetValueFromRepository(key, typeof(T));
                this.cachedValues.TryAdd(key, value);
            }

            return (T)value;
        }

        private object GetValueFromRepository(string key, Type type)
        {
            var stringValue = this.settingRepository.GetSetting(key);
            if (stringValue == null)
            {
                throw new MissingSettingException(string.Format("A setting with the key '{0}' does not exist.", key));
            }

            if (type == typeof(string))
            {
                return stringValue;
            }

            return ConvertValue(stringValue, type);
        }

        private static object ConvertValue(string stringValue, Type type)
        {
            return TypeDescriptor.GetConverter(type).ConvertFromString(stringValue);
        }

    }

2 个答案:

答案 0 :(得分:0)

一种方法是简单地设置值,并捕获在密钥不在集合中时将抛出的异常。

    public bool UpdateSetting<T>(string key, T value)
    {
        try {
            this.cachedValues[key] = value;
        } catch (KeyNotFoundException ex) {
            return false;
        }
        return true;
    }

这是否是您想要处理不存在的密钥的方式取决于您。但是如果您决定要添加密钥,那么您应该使用AddOrUpdate方法而不是上面的简单赋值。在这种情况下,您不需要捕获该异常。

为了解决您的支持存储库的更大问题,我认为您需要一些与此相关的内容。

    public bool UpdateSetting<T>(string key, T value)
    {
        lock {
            try {
                this.cachedValues[key] = value;
                this.settingRepository.Update(... //you'll have to write this
            } catch (KeyNotFoundException ex) { 
                return false;
            }
            return true;
        }
    }

我认为您不能避免使用锁来确保更改缓存与存储库匹配。出于同样的原因,我认为您的一些现有代码也可能需要锁定。使用ConcurrentDictionary仅保护您在字典上的操作。但是在更大的范围内,还有更多的需要同步。

答案 1 :(得分:0)

可能值得获取现有值以避免更新存储库。如果比尝试更贵,那就是例外

public bool UpdateSetting<T>(string key, T value)
{
    lock 
    {
        T oldValue;            
        if (this.cachedValues.TryGetValue(key, out oldValue)
        {
            if (oldValue != value)
            {
                this.cachedValues[key] = value;
                settingRepository.Update(key, value);
            }
            return true;
        } 
        else 
        { 
           return false;
        }            
    }
}