public void CacheUpdateWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); provider.FireCacheUpdated(10, 20); Assert.AreEqual(20L, cacheTotalEntriesCounter.RawValue); Assert.AreEqual(10L, cacheUpdatedEntriesCounter.RawValue); }
public void MultipleCacheUpdatesWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); for (int i = 1; i <= 10; i++) { provider.FireCacheUpdated(i, 10 + i); } Assert.AreEqual(20L, cacheTotalEntriesCounter.RawValue); Assert.AreEqual(55L, cacheUpdatedEntriesCounter.RawValue); // 55 = 10 * (10+1) / 2 }
public void MultipleCacheUpdatesWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); for (int i = 1; i <= 10; i++) { provider.FireCacheUpdated(i, 10 + i); } Assert.AreEqual(20L, cacheTotalEntriesCounter.RawValue); Assert.AreEqual(55L, cacheUpdatedEntriesCounter.RawValue); // 55 = 10 * (10+1) / 2 }
public void CacheUpdateWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); provider.FireCacheUpdated(10, 20); Assert.AreEqual(20L, cacheTotalEntriesCounter.RawValue); Assert.AreEqual(10L, cacheUpdatedEntriesCounter.RawValue); }
/// <summary> /// Add a new keyed object to the cache. /// </summary> /// <param name="key">The key of the object.</param> /// <param name="value">The object to add.</param> /// <param name="scavengingPriority">One of the <see cref="CacheItemPriority"/> values.</param> /// <param name="refreshAction">An <see cref="ICacheItemRefreshAction"/> object.</param> /// <param name="expirations">An array of <see cref="ICacheItemExpiration"/> objects.</param> public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations) { ValidateKey(key); EnsureCacheInitialized(); CacheItem cacheItemBeforeLock = null; bool lockWasSuccessful = false; do { lock (inMemoryCache.SyncRoot) { if (inMemoryCache.Contains(key) == false) { cacheItemBeforeLock = new CacheItem(key, addInProgressFlag, CacheItemPriority.NotRemovable, null); inMemoryCache[key] = cacheItemBeforeLock; } else { cacheItemBeforeLock = (CacheItem)inMemoryCache[key]; } lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock); } if (lockWasSuccessful == false) { Thread.Sleep(0); } } while (lockWasSuccessful == false); try { cacheItemBeforeLock.TouchedByUserAction(true); CacheItem newCacheItem = new CacheItem(key, value, scavengingPriority, refreshAction, expirations); try { backingStore.Add(newCacheItem); cacheItemBeforeLock.Replace(value, refreshAction, scavengingPriority, expirations); inMemoryCache[key] = cacheItemBeforeLock; } catch { backingStore.Remove(key); inMemoryCache.Remove(key); throw; } if (scavengingPolicy.IsScavengingNeeded(inMemoryCache.Count)) { cacheScavenger.StartScavenging(); } instrumentationProvider.FireCacheUpdated(1, inMemoryCache.Count); } finally { Monitor.Exit(cacheItemBeforeLock); } }