Ejemplo n.º 1
0
        /// <summary>
        /// This does not reset the timer!
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public TValue PutIfAbsent(TKey key, TValue value)
        {
            var            newValue = new ExpiringObject(value, Convenient.CurrentTimeMillis(), _timeToLiveSeconds);
            var            segment  = Segment(key);
            ExpiringObject oldValue;

            lock (segment)
            {
                if (!segment.ContainsKey(key))
                {
                    oldValue = segment.Add(key, newValue);
                }
                else
                {
                    oldValue = segment.Get(key);
                    if (oldValue.IsExpired)
                    {
                        segment.Add(key, newValue);
                    }
                    else if (_refreshTimeout)
                    {
                        oldValue = new ExpiringObject(oldValue.Value, Convenient.CurrentTimeMillis(), _timeToLiveSeconds);
                        segment.Add(key, oldValue);
                    }
                }
            }
            if (oldValue == null || oldValue.IsExpired)
            {
                return(null);
            }
            return(oldValue.Value);
        }
Ejemplo n.º 2
0
 public TValue this[TKey key]
 {
     get
     {
         TValue value;
         TryGetValue(key, out value);
         return(value);
     }
     set
     {
         ExpiringObject obj = new ExpiringObject(key, value, DateTime.Now);
         _dict.AddOrUpdate(key, obj, (k, v) => obj);
     }
 }
Ejemplo n.º 3
0
        internal void SetObject(string key, T objectToCache)
        {
            var expiringObject = new ExpiringObject(DateTime.UtcNow, objectToCache);

            objectCache.AddOrUpdate(key, expiringObject,
                                    (newkey, oldValue) =>
            {
                if (oldValue.CachedObject is IDisposable)
                {
                    ((IDisposable)oldValue.CachedObject).Dispose();
                }

                return(expiringObject);
            });
        }
Ejemplo n.º 4
0
        public TValue Put(TKey key, TValue value)
        {
            var            newValue = new ExpiringObject(value, Convenient.CurrentTimeMillis(), _timeToLiveSeconds);
            var            segment  = Segment(key);
            ExpiringObject oldValue;

            lock (segment)
            {
                oldValue = segment.Add(key, newValue);
            }
            if (oldValue == null || oldValue.IsExpired)
            {
                return(null);
            }
            return(oldValue.Value);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Expires a key in a segment. If a key value pair is expired, it will get removed.
 /// </summary>
 /// <param name="segment">The segment.</param>
 /// <param name="key">The key.</param>
 /// <param name="value">The value.</param>
 /// <returns>True, if expired. False, otherwise.</returns>
 private bool Expire(CacheMap <TKey, ExpiringObject> segment, TKey key, ExpiringObject value)
 {
     if (value.IsExpired)
     {
         lock (segment)
         {
             var tmp = segment.Get(key);
             if (tmp != null && tmp.Equals(value))
             {
                 segment.Remove(key);
                 Logger.Debug("Removed in expire: {0}.", value.Value);
                 _removedCounter.IncrementAndGet();
             }
         }
         return(true);
     }
     return(false);
 }
Ejemplo n.º 6
0
        public bool Replace(TKey key, TValue oldValue, TValue newValue)
        {
            var oldValue2 = new ExpiringObject(oldValue, 0, _timeToLiveSeconds);
            var newValue2 = new ExpiringObject(newValue, Convenient.CurrentTimeMillis(), _timeToLiveSeconds);

            var            segment = Segment(key);
            ExpiringObject oldValue3;
            bool           replaced = false;

            lock (segment)
            {
                oldValue3 = segment.Get(key);
                if (oldValue3 != null && !oldValue3.IsExpired && oldValue2.Value.Equals(oldValue3.Value))
                {
                    segment.Add(key, newValue2);
                    replaced = true;
                }
            }
            if (oldValue3 != null)
            {
                Expire(segment, key, oldValue3);
            }
            return(replaced);
        }