示例#1
0
        public bool TryGetValue(TKey key, out TValue value)
        {
            TValue o;

            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))
                {
                    TimedCacheKey <TKey> tkey = timedStorageIndex[key];
                    o = timedStorage[tkey];
                    timedStorage.Remove(tkey);
                    tkey.Accessed();
                    timedStorage.Add(tkey, o);
                    value = o;
                    return(true);
                }
            }
            finally { Monitor.Exit(syncRoot); }

            value = default(TValue);
            return(false);
        }
示例#2
0
 public object this[TKey key]
 {
     get
     {
         TValue o;
         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))
             {
                 TimedCacheKey <TKey> tkey = timedStorageIndex[key];
                 o = timedStorage[tkey];
                 timedStorage.Remove(tkey);
                 tkey.Accessed();
                 timedStorage.Add(tkey, o);
                 return(o);
             }
             else
             {
                 throw new ArgumentException("Key not found in the cache");
             }
         }
         finally { Monitor.Exit(syncRoot); }
     }
 }