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 Update(object key, object value, DateTime expiration) { // Synchronise access to storage structures. A read lock may // already be acquired before this method is called. bool LockUpgraded = readWriteLock.IsReaderLockHeld; LockCookie lc = new LockCookie(); if (LockUpgraded) { lc = readWriteLock.UpgradeToWriterLock(MAX_LOCK_WAIT); } else { readWriteLock.AcquireWriterLock(MAX_LOCK_WAIT); } try { if (untimedStorage.ContainsKey(key)) { untimedStorage.Remove(key); } else if (timedStorageIndex.ContainsKey(key)) { timedStorage.Remove(timedStorageIndex[key]); timedStorageIndex.Remove(key); } else { return(false); } TimedCacheKey internalKey = new TimedCacheKey(key, expiration); timedStorage.Add(internalKey, value); timedStorageIndex.Add(key, internalKey); return(true); } finally { // Restore lock state if (LockUpgraded) { readWriteLock.DowngradeFromWriterLock(ref lc); } else { readWriteLock.ReleaseWriterLock(); } } }
public bool Add(object key, object value, TimeSpan slidingExpiration) { // Synchronise access to storage structures. A read lock may // already be acquired before this method is called. bool LockUpgraded = readWriteLock.IsReaderLockHeld; LockCookie lc = new LockCookie(); if (LockUpgraded) { lc = readWriteLock.UpgradeToWriterLock(MAX_LOCK_WAIT); } else { readWriteLock.AcquireWriterLock(MAX_LOCK_WAIT); } try { // This is the actual adding of the key if (timedStorageIndex.ContainsKey(key)) { return(false); } else { TimedCacheKey internalKey = new TimedCacheKey(key, slidingExpiration); timedStorage.Add(internalKey, value); timedStorageIndex.Add(key, internalKey); return(true); } } finally { // Restore lock state if (LockUpgraded) { readWriteLock.DowngradeFromWriterLock(ref lc); } else { readWriteLock.ReleaseWriterLock(); } } }
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(); } }
public bool Update(object key, object value, TimeSpan slidingExpiration) { // Synchronise access to storage structures. A read lock may // already be acquired before this method is called. bool LockUpgraded = readWriteLock.IsReaderLockHeld; LockCookie lc = new LockCookie(); if (LockUpgraded) { lc = readWriteLock.UpgradeToWriterLock(MAX_LOCK_WAIT); } else { readWriteLock.AcquireWriterLock(MAX_LOCK_WAIT); } try { if (untimedStorage.ContainsKey(key)) { untimedStorage.Remove(key); } else if (timedStorageIndex.ContainsKey(key)) { timedStorage.Remove(timedStorageIndex[key]); timedStorageIndex.Remove(key); } else { return false; } TimedCacheKey internalKey = new TimedCacheKey(key, slidingExpiration); timedStorage.Add(internalKey, value); timedStorageIndex.Add(key, internalKey); return true; } finally { // Restore lock state if (LockUpgraded) { readWriteLock.DowngradeFromWriterLock(ref lc); } else { readWriteLock.ReleaseWriterLock(); } } }
public bool Add(object key, object value, DateTime expiration) { // Synchronise access to storage structures. A read lock may // already be acquired before this method is called. bool LockUpgraded = readWriteLock.IsReaderLockHeld; LockCookie lc = new LockCookie(); if (LockUpgraded) { lc = readWriteLock.UpgradeToWriterLock(MAX_LOCK_WAIT); } else { readWriteLock.AcquireWriterLock(MAX_LOCK_WAIT); } try { // This is the actual adding of the key if (timedStorageIndex.ContainsKey(key)) { return false; } else { TimedCacheKey internalKey = new TimedCacheKey(key, expiration); timedStorage.Add(internalKey, value); timedStorageIndex.Add(key, internalKey); return true; } } finally { // Restore lock state if (LockUpgraded) { readWriteLock.DowngradeFromWriterLock(ref lc); } else { readWriteLock.ReleaseWriterLock(); } } }
/// <summary> /// Purges expired objects from the cache. Called automatically by the purge timer.--检测缓存信息是否过期的计时器触发执行的方法 /// </summary> private void PurgeCache(object sender, System.Timers.ElapsedEventArgs e) { // Note: This implementation runs with low priority. If the cache lock // is heavily contended (many threads) the purge will take a long time // to obtain the lock it needs and may never be run. System.Threading.Thread.CurrentThread.Priority = ThreadPriority.BelowNormal; // Only let one thread purge at once - a buildup could cause a crash // This could cause the purge to be delayed while there are lots of read/write ops // happening on the cache if (!Monitor.TryEnter(isPurging)) { return; } try { readWriteLock.AcquireWriterLock(MAX_LOCK_WAIT); try { List <object> expiredItems = new List <object>(); /* * DateTime startTime = DateTime.Now; * System.Console.WriteLine("Purge " + " started at " + startTime.ToLongTimeString()); */ foreach (TimedCacheKey timedKey in timedStorage.Keys) { if (timedKey.ExpirationDate < e.SignalTime) { // Mark the object for purge expiredItems.Add(timedKey.Key); } else { break; } } foreach (object key in expiredItems) { TimedCacheKey timedKey = timedStorageIndex[key]; timedStorageIndex.Remove(timedKey.Key); timedStorage.Remove(timedKey); } /* * DateTime endTime = DateTime.Now; * System.Console.WriteLine("Purge completed at " + endTime.ToLongTimeString()); * System.Console.WriteLine("Time taken to complete purge was " + TimeSpan.FromTicks(endTime.Ticks - startTime.Ticks)); */ } catch (ApplicationException ae) { // Unable to obtain write lock to the timed cache storage object System.Console.WriteLine("Unable to complete cache purge, could not get writer lock."); } finally { readWriteLock.ReleaseWriterLock(); } } finally { Monitor.Exit(isPurging); } }