/// <summary> /// Get the object from the cache for the key. /// </summary> /// <param name="key"> /// The key whose value to get. /// </param> /// <returns> /// The value associated with the specified key. /// </returns> public object GetData(string key) { ValidateKey(key); CacheItem cacheItemBeforeLock = null; bool lockWasSuccessful = false; do { lock (inMemoryCache.SyncRoot) { cacheItemBeforeLock = (CacheItem)inMemoryCache[key]; if (IsObjectInCache(cacheItemBeforeLock)) { instrumentationProvider.FireCacheAccessed(key, false); return(null); } lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock); } if (lockWasSuccessful == false) { Thread.Sleep(0); } } while (lockWasSuccessful == false); try { if (cacheItemBeforeLock.HasExpired()) { cacheItemBeforeLock.TouchedByUserAction(true); backingStore.Remove(key); // Does exception safety matter here? We're removing it due to expiration or scavenging... inMemoryCache.Remove(key); RefreshActionInvoker.InvokeRefreshAction(cacheItemBeforeLock, CacheItemRemovedReason.Expired, instrumentationProvider); instrumentationProvider.FireCacheAccessed(key, false); instrumentationProvider.FireCacheUpdated(1, inMemoryCache.Count); instrumentationProvider.FireCacheExpired(1); return(null); } backingStore.UpdateLastAccessedTime(cacheItemBeforeLock.Key, DateTime.Now); // Does exception safety matter here? cacheItemBeforeLock.TouchedByUserAction(false); instrumentationProvider.FireCacheAccessed(key, true); return(cacheItemBeforeLock.Value); } finally { Monitor.Exit(cacheItemBeforeLock); } }
public void MultipleCacheAccessWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); for (int i = 0; i < 10; i++) { provider.FireCacheAccessed("key", true); provider.FireCacheAccessed("key", false); provider.FireCacheAccessed("key", true); } Assert.AreEqual(20L, cacheHitsCounter.RawValue); Assert.AreEqual(10L, cacheMissesCounter.RawValue); Assert.AreEqual(66.666f, cacheHitRatioCounter.NextValue(), 0.01f); Assert.AreEqual(30L, cacheAccessAttemptsCounter.RawValue); }
public void CacheMissWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); provider.FireCacheAccessed("key", false); Assert.AreEqual(0L, cacheHitsCounter.RawValue); Assert.AreEqual(1L, cacheMissesCounter.RawValue); Assert.AreEqual(0f, cacheHitRatioCounter.NextValue(), 0.01f); Assert.AreEqual(1L, cacheAccessAttemptsCounter.RawValue); }
public void MultipleCacheAccessWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); for (int i = 0; i < 10; i++) { provider.FireCacheAccessed("key", true); provider.FireCacheAccessed("key", false); provider.FireCacheAccessed("key", true); } Assert.AreEqual(20L, cacheHitsCounter.RawValue); Assert.AreEqual(10L, cacheMissesCounter.RawValue); Assert.AreEqual(66.666f, cacheHitRatioCounter.NextValue(), 0.01f); Assert.AreEqual(30L, cacheAccessAttemptsCounter.RawValue); }
public void CacheMissWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); provider.FireCacheAccessed("key", false); Assert.AreEqual(0L, cacheHitsCounter.RawValue); Assert.AreEqual(1L, cacheMissesCounter.RawValue); Assert.AreEqual(0f, cacheHitRatioCounter.NextValue(), 0.01f); Assert.AreEqual(1L, cacheAccessAttemptsCounter.RawValue); }