Example #1
0
 /// <summary>
 /// Adiciona um novo item.
 /// </summary>
 /// <param name="key">Instancia do item.</param>
 /// <param name="expirationSeconds">Tempo de vida em segundos.</param>
 /// <returns></returns>
 public bool Add(TKey key, double expirationSeconds)
 {
     if (!Monitor.TryEnter(_syncRoot, 5000))
     {
         throw new ApplicationException("Lock could not be acquired after " + 5000 + "ms");
     }
     try
     {
         if (_timedStorageIndex.ContainsKey(key))
         {
             return(false);
         }
         else
         {
             var internalKey = new TimedCacheKey <TKey>(key, DateTime.UtcNow + TimeSpan.FromSeconds(expirationSeconds));
             _timedStorage.Add(internalKey);
             _timedStorageIndex.Add(key, internalKey);
             return(true);
         }
     }
     finally
     {
         Monitor.Exit(_syncRoot);
     }
 }
Example #2
0
 /// <summary>
 /// Atualiza o item.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="slidingExpiration"></param>
 /// <returns></returns>
 public bool Update(TKey key, TimeSpan slidingExpiration)
 {
     if (!Monitor.TryEnter(_syncRoot, 5000))
     {
         throw new ApplicationException("Lock could not be acquired after " + 5000 + "ms");
     }
     try
     {
         if (_timedStorageIndex.ContainsKey(key))
         {
             _timedStorage.Remove(_timedStorageIndex[key]);
             _timedStorageIndex.Remove(key);
         }
         else
         {
             return(false);
         }
         TimedCacheKey <TKey> internalKey = new TimedCacheKey <TKey>(key, slidingExpiration);
         _timedStorage.Add(internalKey);
         _timedStorageIndex.Add(key, internalKey);
         return(true);
     }
     finally
     {
         Monitor.Exit(_syncRoot);
     }
 }
Example #3
0
 public TKey this[int i]
 {
     get
     {
         TKey o;
         if (!Monitor.TryEnter(_syncRoot, 5000))
         {
             throw new ApplicationException("Lock could not be acquired after " + 5000 + "ms");
         }
         try
         {
             if (_timedStorage.Count > i)
             {
                 TimedCacheKey <TKey> tkey = _timedStorage[i];
                 o = tkey.Key;
                 _timedStorage.Remove(tkey);
                 tkey.Accessed();
                 _timedStorage.Insert(i, tkey);
                 return(o);
             }
             else
             {
                 throw new ArgumentException("Key not found in the cache");
             }
         }
         finally
         {
             Monitor.Exit(_syncRoot);
         }
     }
     set
     {
         AddOrUpdate(value, _defaultTime);
     }
 }
Example #4
0
        /// <summary>
        /// Expurga os objetos expirados a partir do cache. Chamado automaticamente pelo temporizador.
        /// </summary>
        private void PurgeCache(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (!Monitor.TryEnter(_isPurging))
            {
                return;
            }
            DateTime signalTime = DateTime.UtcNow;

            try
            {
                if (!Monitor.TryEnter(_syncRoot, 5000))
                {
                    return;
                }
                try
                {
                    Lazy <List <object> > expiredItems = new Lazy <List <object> >();
                    foreach (TimedCacheKey <TKey> timedKey in _timedStorage)
                    {
                        if (timedKey.ExpirationDate < signalTime)
                        {
                            expiredItems.Value.Add(timedKey.Key);
                        }
                    }
                    if (expiredItems.IsValueCreated)
                    {
                        foreach (TKey key in expiredItems.Value)
                        {
                            TimedCacheKey <TKey> timedKey = _timedStorageIndex[key];
                            _timedStorageIndex.Remove(timedKey.Key);
                            _timedStorage.Remove(timedKey);
                            OnRemoved(key);
                        }
                    }
                }
                finally
                {
                    Monitor.Exit(_syncRoot);
                }
            }
            finally
            {
                Monitor.Exit(_isPurging);
            }
        }