Ejemplo n.º 1
0
 public bool Add(TKey key, double expirationSeconds)
 {
     if (!Monitor.TryEnter(syncRoot, MAX_LOCK_WAIT))
     {
         throw new ApplicationException("Lock could not be acquired after " + MAX_LOCK_WAIT + "ms");
     }
     try
     {
         // This is the actual adding of the key
         if (timedStorageIndex.ContainsKey(key))
         {
             return(false);
         }
         else
         {
             TimedCacheKey <TKey> internalKey = new TimedCacheKey <TKey>(key,
                                                                         DateTime.UtcNow +
                                                                         TimeSpan.FromSeconds(expirationSeconds));
             timedStorage.Add(internalKey);
             timedStorageIndex.Add(key, internalKey);
             return(true);
         }
     }
     finally
     {
         Monitor.Exit(syncRoot);
     }
 }
Ejemplo n.º 2
0
        public bool Update(TKey key, TimeSpan slidingExpiration)
        {
            if (!Monitor.TryEnter(syncRoot, MAX_LOCK_WAIT))
            {
                throw new ApplicationException("Lock could not be acquired after " + MAX_LOCK_WAIT + "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);
            }
        }
Ejemplo n.º 3
0
 public TKey this[int i]
 {
     get
     {
         TKey o;
         if (!Monitor.TryEnter(syncRoot, MAX_LOCK_WAIT))
         {
             throw new ApplicationException("Lock could not be acquired after " + MAX_LOCK_WAIT + "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); }
 }