Example #1
0
        /// <summary>
        ///     Removes all expired items from the dictionary.
        /// </summary>
        public void RemoveExpiredItems()
        {
            var removeList = innerDictionary.Where(kvp => kvp.Value.HasExpired).ToList();

            foreach (var kvp in removeList)
            {
                ItemExpired?.Invoke(this, new ExpirableItemRemovedEventArgs <TKey, TValue>(kvp.Key, kvp.Value.Value));
                innerDictionary.Remove(kvp.Key);
            }
        }
Example #2
0
        public void ExpireCacheItems()
        {
            var now = DateTimeOffset.UtcNow;

            Parallel.ForEach(_cacheItems, item =>
            {
                if (now - item.Value.LastAccessed >= _cacheExpirationDuration)
                {
                    CacheItem <K, V> itemToRemove;
                    _cacheItems.TryRemove(item.Key, out itemToRemove);
                    ItemExpired?.Invoke(this, itemToRemove);
                }
            });
        }
 public bool ContainsKey(K key)
 {
     lock (lockObject)
     {
         if (CacheItems.ContainsKey(key))
         {
             if (CacheItems[key].HasExpired)
             {
                 ItemExpired?.Invoke(this, new CacheItemRemovedEventArgs <K, T>
                 {
                     Key   = key,
                     Value = CacheItems[key].Value
                 });
                 CacheItems.Remove(key);
                 return(false);
             }
             return(true);
         }
         return(false);
     }
 }
Example #4
0
        /// <summary>
        ///     Tries to the get item having the specified key. Returns <c>true</c> if the item exists and has not expired.
        /// </summary>
        public bool TryGetValue(TKey key, out TValue value)
        {
            ExpirableItem <TValue> item;

            if (innerDictionary.TryGetValue(key, out item))
            {
                if (item.HasExpired)
                {
                    ItemExpired?.Invoke(this, new ExpirableItemRemovedEventArgs <TKey, TValue>(key, item.Value));
                    innerDictionary.Remove(key);

                    value = default(TValue);
                    return(false);
                }

                value = item.Value;
                return(true);
            }

            value = default(TValue);
            return(false);
        }