public object Get(object key) { object o; readWriteLock.AcquireReaderLock(MAX_LOCK_WAIT); try { if (untimedStorage.ContainsKey(key)) { return(untimedStorage[key]); } LockCookie lc = readWriteLock.UpgradeToWriterLock(MAX_LOCK_WAIT); try { if (timedStorageIndex.ContainsKey(key)) { TimedCacheKey tkey = timedStorageIndex[key]; o = timedStorage[tkey]; timedStorage.Remove(tkey); tkey.Accessed(); timedStorage.Add(tkey, o); return(o); } else { throw new ArgumentException("Key [" + key + "] not found in the cache"); } } finally { readWriteLock.DowngradeFromWriterLock(ref lc); } } finally { readWriteLock.ReleaseReaderLock(); } }
public bool TryGet(object key, out object value) { object o; readWriteLock.AcquireReaderLock(MAX_LOCK_WAIT); try { if (untimedStorage.ContainsKey(key)) { value = untimedStorage[key]; return(true); } LockCookie lc = readWriteLock.UpgradeToWriterLock(MAX_LOCK_WAIT); try { if (timedStorageIndex.ContainsKey(key)) { TimedCacheKey tkey = timedStorageIndex[key]; o = timedStorage[tkey]; timedStorage.Remove(tkey); tkey.Accessed(); timedStorage.Add(tkey, o); value = o; return(true); } else { value = null; return(false); } } finally { readWriteLock.DowngradeFromWriterLock(ref lc); } } finally { readWriteLock.ReleaseReaderLock(); } }