加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何实现定期缓存重新加载

发布时间:2020-12-15 22:51:28 所属栏目:百科 来源:网络整理
导读:public class CacheTKey,TValue : ICacheTKey,TValue{ private readonly IDictionaryTKey,TValue _internalCache; private readonly object _syncLock = new object(); public Cache() { _internalCache = new DictionaryTKey,TValue(); } public TValue thi
public class Cache<TKey,TValue> : ICache<TKey,TValue>
{
    private readonly IDictionary<TKey,TValue> _internalCache;
    private readonly object _syncLock = new object();

    public Cache()
    {
        _internalCache = new Dictionary<TKey,TValue>();
    }

    public TValue this[TKey key]
    {
        get
        {
            lock (_syncLock) {
               //...
            }
        }
        set
        {
            lock (_syncLock) {
               //...
            }
        }
    }

    public ICollection<TValue> GetAll()
    {
        lock (_syncLock) {
            return _internalCache.Values;
        }
    }

    public bool ContainsKey(TKey key)
    {
        lock (_syncLock)
        {
            return _internalCache.ContainsKey(key);
        }
    }

}

上述缓存由单例包装器使用.以下是实施.

public class ActivityCache : ICache<string,Activity> 
 {
    private readonly ICache<string,Activity> _cache = new Cache<string,Activity>();

    private static readonly ActivityCache _instance = new ActivityCache();

    // http://www.yoda.arachsys.com/csharp/singleton.html
    static ActivityCache()
    {
    }

    ActivityCache()
    {
    }

    public static ActivityCache Instance
    {
        get { return _instance; }
    }

    public Activity this[string activityUrl]
    {
        get
        {
            if (string.IsNullOrEmpty(activityUrl))
            {
                return null;
            }

            return _cache[activityUrl];
        }
        set
        {
            if (string.IsNullOrEmpty(activityUrl))
            {
                return;
            }

            _cache[activityUrl] = value;
        }
    }

    public ICollection<Activity> GetAll()
    {
        return _cache.GetAll();
    }

    public bool ContainsKey(string key)
    {
        return _cache.ContainsKey(key);
    }
}

现在我的问题是,如果我想重新加载缓存,我可以使用以下?我希望无缝重新加载,以便线程可以访问陈旧信息.

public void Reload(IDictionary<TKey,TValue> values)
{
    cache = new Dictionary<TKey,TValue> ();
    foreach (KeyValuePair<TKey,TValue> value in values)
    {
        cache[value.Key] = value.Value;
    }

    lock (_syncLock)
    {
        _internalCache = cache;
    }
}

_internalCache是??readonly.它不会让我改变任务.

解决方法

_internalCache是??只读的(我假设您不只是想删除readonly关键字以使其工作).但这只会影响对列表的引用.您仍然可以替换列表中的项目,如下所示:

public void Reload(IDictionary<TKey,TValue> values)
{
    lock(_synclock)
    {
        _internalCache.Clear();
        _internalCache.AddRange(values);
    }
}

此外,您可以考虑使用ConcurrentDictionary提供以安全方式访问密钥的方法,而不是自己编写所有这些锁,甚至允许您提供一个委托,以便在缺少密钥时重新填充条目.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读