Пример #1
0
        public TValue TrySet(TKey key, Func <TKey, TValue> valueGetter, ICachePolicy policy)
        {
            _rwLock.EnterReadLock();
            try {
                if (_contents.TryGetValue(key, out var cached))
                {
                    return(cached.Value);
                }
            }
            finally {
                _rwLock.ExitReadLock();
            }

            _rwLock.EnterWriteLock();
            try {
                if (_contents.TryGetValue(key, out var cached))
                {
                    return(cached.Value);
                }

                cached = new CacheContent <TKey, TValue>(key, valueGetter(key), policy);
                UpdateExpiryControl(cached.ExpiresAt, key);
                return(cached.Value);
            }
            finally {
                _rwLock.ExitWriteLock();
            }
        }
Пример #2
0
 public void Set(TKey key, TValue value, ICachePolicy policy)
 {
     _rwLock.EnterWriteLock();
     try {
         var cached = new CacheContent <TKey, TValue>(key, value, policy);
         if (_contents.ContainsKey(key))
         {
             _contents[key] = cached;
         }
         else
         {
             _contents.Add(key, cached);
         }
         UpdateExpiryControl(cached.ExpiresAt, key);
     }
     finally {
         _rwLock.ExitWriteLock();
     }
 }