protected override void ExitLock() { if (CacheLock.IsReadLockHeld) { CacheLock.ExitReadLock(); } }
/// <summary> /// Count all cached values /// </summary> /// <returns></returns> public int Count() { CacheLock.EnterReadLock(); try { return(Cache.Count); } finally { CacheLock.ExitReadLock(); } }
/// <summary> /// Try to get cached value /// Return false if no exist value or exist value expired /// </summary> /// <param name="key">Cache key</param> /// <param name="value">Cache value</param> /// <returns></returns> public bool TryGetValue(TKey key, out TValue value) { RevokeExpires(); var now = DateTime.UtcNow; CacheLock.EnterReadLock(); try { Pair <TValue, DateTime> pair; if (Cache.TryGetValue(key, out pair) && pair.Second > now) { value = pair.First; return(true); } else { value = default(TValue); return(false); } } finally { CacheLock.ExitReadLock(); } }