/// <summary> /// Made public for testing purposes. /// </summary> public CacheManager BuildCacheManager( string cacheManagerName, IBackingStore backingStore, int maximumElementsInCacheBeforeScavenging, int numberToRemoveWhenScavenging, int expirationPollFrequencyInSeconds, CachingInstrumentationProvider instrumentationProvider) { CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy(maximumElementsInCacheBeforeScavenging); Cache cache = new Cache(backingStore, scavengingPolicy, instrumentationProvider); ExpirationPollTimer timer = new ExpirationPollTimer(); ExpirationTask expirationTask = CreateExpirationTask(cache, instrumentationProvider); ScavengerTask scavengerTask = new ScavengerTask(numberToRemoveWhenScavenging, scavengingPolicy, cache, instrumentationProvider); BackgroundScheduler scheduler = new BackgroundScheduler(expirationTask, scavengerTask, instrumentationProvider); cache.Initialize(scheduler); scheduler.Start(); timer.StartPolling(new TimerCallback(scheduler.ExpirationTimeoutExpired), expirationPollFrequencyInSeconds * 1000); return(new CacheManager(cache, scheduler, timer)); }
void IContainerPolicyCreator.CreatePolicies( IPolicyList policyList, string instanceName, ConfigurationElement configurationObject, IConfigurationSource configurationSource) { CacheManagerData castConfigurationObject = (CacheManagerData)configurationObject; var cacheManagerName = instanceName; var cacheStorageName = castConfigurationObject.CacheStorage; var maximumElementsInCacheBeforeScavenging = castConfigurationObject.MaximumElementsInCacheBeforeScavenging; var numberToRemoveWhenScavenging = castConfigurationObject.NumberToRemoveWhenScavenging; var expirationPollFrequencyInSeconds = castConfigurationObject.ExpirationPollFrequencyInSeconds; policyList.Set <IBuildPlanPolicy>( new DelegateBuildPlanPolicy( context => { IBuilderContext backingStoreContext = context.CloneForNewBuild(NamedTypeBuildKey.Make <IBackingStore>(cacheStorageName), null); IBackingStore backingStore = (IBackingStore)context.Strategies.ExecuteBuildUp(backingStoreContext); CachingInstrumentationProvider instrumentationProvider = CreateInstrumentationProvider(cacheManagerName, configurationSource); return(new CacheManagerFactoryHelper().BuildCacheManager( cacheManagerName, backingStore, maximumElementsInCacheBeforeScavenging, numberToRemoveWhenScavenging, expirationPollFrequencyInSeconds, instrumentationProvider)); }), NamedTypeBuildKey.Make <CacheManager>(cacheManagerName)); }
public void ItemAddedPreviousToFailedAddIsRemovedCompletelyIfSecondAddFails() { using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo", null)) { CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy(10); CachingInstrumentationProvider instrumentationProvider = new CachingInstrumentationProvider(); Cache cache = new Cache(backingStore, scavengingPolicy, instrumentationProvider); cache.Initialize(this); cache.Add("my", new SerializableClass()); try { cache.Add("my", new NonSerializableClass()); Assert.Fail("Should have thrown exception internally to Cache.Add"); } catch (Exception) { Assert.IsFalse(cache.Contains("my")); Assert.AreEqual(0, backingStore.Count); Hashtable isolatedStorageContents = backingStore.Load(); Assert.AreEqual(0, isolatedStorageContents.Count); } } }
private static CachingInstrumentationProvider CreateInstrumentationProvider(string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { CachingInstrumentationProvider instrumentationProvider = new CachingInstrumentationProvider(); new InstrumentationAttachmentStrategy().AttachInstrumentation(name, instrumentationProvider, configurationSource, reflectionCache); return(instrumentationProvider); }
public void Reset() { removedValue = "Known bad value"; callbackHappened = false; callbackReason = CacheItemRemovedReason.Unknown; removedKey = null; instrumentationProvider = new CachingInstrumentationProvider(); }
public RefreshActionData(ICacheItemRefreshAction refreshAction, string keyToRefresh, object removedData, CacheItemRemovedReason removalReason, CachingInstrumentationProvider instrumentationProvider) { this.refreshAction = refreshAction; this.keyToRefresh = keyToRefresh; this.removalReason = removalReason; this.removedData = removedData; this.instrumentationProvider = instrumentationProvider; }
public void CacheExpirationWithPerformanceCountersDisabledDoesnNotUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, false, false, formatter); provider.FireCacheExpired(10); Assert.AreEqual(0L, cacheExpiriesCounter.RawValue); }
public void TestInitialize() { inMemoryCache = new Hashtable(); instrumentationProvider = new CachingInstrumentationProvider(); expirer = new ExpirationTask(this, instrumentationProvider); expiredItemKeys = ""; callbackCount = 0; callbackReason = CacheItemRemovedReason.Unknown; }
public void CacheScavengingWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); provider.FireCacheScavenged(10); Assert.AreEqual(10L, cacheScavengedItemsCounter.RawValue); }
public ScavengerTask(int numberToRemoveWhenScavenging, CacheCapacityScavengingPolicy scavengingPolicy, ICacheOperations cacheOperations, CachingInstrumentationProvider instrumentationProvider) { this.numberToRemoveWhenScavenging = numberToRemoveWhenScavenging; this.scavengingPolicy = scavengingPolicy; this.cacheOperations = cacheOperations; this.instrumentationProvider = instrumentationProvider; }
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 BackgroundScheduler(ExpirationTask expirer, ScavengerTask scavenger, CachingInstrumentationProvider instrumentationProvider) { this.expirer = expirer; this.scavenger = scavenger; this.instrumentationProvider = instrumentationProvider; this.ignoredScavengeRequestsCount = 0; ThreadStart queueReader = new ThreadStart(QueueReader); inputQueueThread = new Thread(queueReader); inputQueueThread.IsBackground = true; }
/// <summary> /// Initialzie a new instance of a <see cref="Cache"/> class with a backing store, and scavenging policy. /// </summary> /// <param name="backingStore">The cache backing store.</param> /// <param name="scavengingPolicy">The scavenging policy.</param> /// <param name="instrumentationProvider">The instrumentation provider.</param> public Cache(IBackingStore backingStore, CacheCapacityScavengingPolicy scavengingPolicy, CachingInstrumentationProvider instrumentationProvider) { this.backingStore = backingStore; this.scavengingPolicy = scavengingPolicy; this.instrumentationProvider = instrumentationProvider; Hashtable initialItems = backingStore.Load(); inMemoryCache = Hashtable.Synchronized(initialItems); this.instrumentationProvider.FireCacheUpdated(initialItems.Count, initialItems.Count); }
public void MultipleCacheScavengingsWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); for (int i = 1; i <= 10; i++) { provider.FireCacheScavenged(10); } Assert.AreEqual(100L, cacheScavengedItemsCounter.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 SetUp() { instrumentationProvider = new CachingInstrumentationProvider(); instrumentationListener = new MockCachingInstrumentationListener(); ReflectionInstrumentationBinder binder = new ReflectionInstrumentationBinder(); binder.Bind(instrumentationProvider, instrumentationListener); backingStore = new NullBackingStore(); factoryHelper = new MockCacheManagerFactoryHelper(); }
public void CacheFailureWithInstrumentationDisabledDoesNotWriteToEventLog() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, false, false, formatter); Exception exception = new Exception(exceptionMessage); using (var eventLog = new EventLogTracker(GetEventLog())) { provider.FireCacheFailed(errorMessage, exception); Assert.AreEqual(0, eventLog.NewEntries().Count()); } }
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 CacheFailureWithInstrumentationDisabledDoesNotWriteToEventLog() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, false, false, formatter); Exception exception = new Exception(exceptionMessage); using (var eventLog = new EventLogTracker(GetEventLog())) { provider.FireCacheFailed(errorMessage, exception); Assert.AreEqual(0, eventLog.NewEntries().Count()); } }
public void CacheCallbackFailureWithInstrumentationDisabledDoesNotWriteToEventLog() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, false, false, formatter); Exception exception = new Exception(exceptionMessage); using (var eventLog = new EventLogTracker(GetEventLog())) { provider.FireCacheCallbackFailed(errorMessage, exception); var newEntries = from entry in eventLog.NewEntries() where entry.Message.IndexOf(exceptionMessage) > -1 select entry; Assert.AreEqual(0, newEntries.Count()); } }
public void CacheCallbackFailureWithInstrumentationEnabledDoesWriteToEventLog() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, false, true, formatter); Exception exception = new Exception(exceptionMessage); using (var eventLog = new EventLogTracker(GetEventLog())) { provider.FireCacheCallbackFailed(key, exception); var newEntries = from entry in eventLog.NewEntries() where entry.Message.IndexOf(exceptionMessage) > -1 select entry; Assert.AreEqual(1, newEntries.Count()); } }
public ICacheManager Assemble(IBuilderContext context, CacheManagerDataBase objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { CacheManagerData cacheManagerData = (CacheManagerData)objectConfiguration; IBackingStore backingStore = BackingStoreCustomFactory.Instance.Create(context, cacheManagerData.CacheStorage, configurationSource, reflectionCache); CachingInstrumentationProvider instrumentationProvider = CreateInstrumentationProvider(cacheManagerData.Name, configurationSource, reflectionCache); CacheManager createdObject = new CacheManagerFactoryHelper().BuildCacheManager( cacheManagerData.Name, backingStore, cacheManagerData.MaximumElementsInCacheBeforeScavenging, cacheManagerData.NumberToRemoveWhenScavenging, cacheManagerData.ExpirationPollFrequencyInSeconds, instrumentationProvider); return(createdObject); }
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 ExceptionThrownDuringAddResultsInObjectBeingRemovedFromCacheCompletely() { MockBackingStore backingStore = new MockBackingStore(); CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy(10); CachingInstrumentationProvider instrumentationProvider = new CachingInstrumentationProvider(); Cache cache = new Cache(backingStore, scavengingPolicy, instrumentationProvider); cache.Initialize(this); try { cache.Add("foo", "bar"); Assert.Fail("Should have thrown exception thrown internally to Cache.Add"); } catch (Exception) { Assert.IsFalse(cache.Contains("foo")); Assert.AreEqual(1, backingStore.removalCount); } }
public void ExceptionThrownDuringAddIntoIsolatedStorageAllowsItemToBeReaddedLater() { using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo", null)) { CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy(10); CachingInstrumentationProvider instrumentationProvider = new CachingInstrumentationProvider(); Cache cache = new Cache(backingStore, scavengingPolicy, instrumentationProvider); cache.Initialize(this); try { cache.Add("my", new NonSerializableClass()); Assert.Fail("Should have thrown exception internally to Cache.Add"); } catch (Exception) { cache.Add("my", new SerializableClass()); Assert.IsTrue(cache.Contains("my")); } } }
/// <summary> /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code. /// Builds a <see cref="CacheManagerData"/> described by the <see cref="CacheManagerSettings"/> configuration section. /// </summary> /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param> /// <param name="name">The name of the instance to build. It is part of the <see cref="ICustomFactory.CreateObject(IBuilderContext, string, IConfigurationSource, ConfigurationReflectionCache)"/> method, but it is not used in this implementation.</param> /// <param name="configurationSource">The source for configuration objects.</param> /// <param name="reflectionCache">The cache to use retrieving reflection information.</param> /// <returns>A fully initialized instance of <see cref="CacheManager"/>.</returns> public object CreateObject(IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { CacheManagerData objectConfiguration = GetConfiguration(name, configurationSource); IBackingStore backingStore = BackingStoreCustomFactory.Instance.Create(context, objectConfiguration.CacheStorage, configurationSource, reflectionCache); CachingInstrumentationProvider instrumentationProvider = CreateInstrumentationProvider(objectConfiguration.Name, configurationSource, reflectionCache); CacheManager createdObject = new CacheManagerFactoryHelper().BuildCacheManager( objectConfiguration.Name, backingStore, objectConfiguration.MaximumElementsInCacheBeforeScavenging, objectConfiguration.NumberToRemoveWhenScavenging, objectConfiguration.ExpirationPollFrequencyInSeconds, instrumentationProvider); RegisterObject(context, name, createdObject); return(createdObject); }
public void MultipleCacheScavengingsWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); for (int i = 1; i <= 10; i++) { provider.FireCacheScavenged(10); } Assert.AreEqual(100L, cacheScavengedItemsCounter.RawValue); }
public void CacheScavengingWithPerformanceCountersEnabledDoesUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, true, false, formatter); provider.FireCacheScavenged(10); Assert.AreEqual(10L, cacheScavengedItemsCounter.RawValue); }
public void CacheExpirationWithPerformanceCountersDisabledDoesnNotUpdateCounters() { ICachingInstrumentationProvider provider = new CachingInstrumentationProvider(instanceName, false, false, formatter); provider.FireCacheExpired(10); Assert.AreEqual(0L, cacheExpiriesCounter.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); }
/// <summary> /// Initialize an instance of the <see cref="ExpirationTask"/> class with an <see cref="ICacheOperations"/> object. /// </summary> /// <param name="cacheOperations">An <see cref="ICacheOperations"/> object.</param> /// <param name="instrumentationProvider">An instrumentation provider.</param> public ExpirationTask(ICacheOperations cacheOperations, CachingInstrumentationProvider instrumentationProvider) { this.cacheOperations = cacheOperations; this.instrumentationProvider = instrumentationProvider; }
public NullExpirationTask(ICacheOperations cacheOperations, CachingInstrumentationProvider instrumentationProvider) : base(cacheOperations, instrumentationProvider) { }
protected internal override ExpirationTask CreateExpirationTask(ICacheOperations cacheOperations, CachingInstrumentationProvider instrumentationProvider) { return(doExpirations ? base.CreateExpirationTask(cacheOperations, instrumentationProvider) : new NullExpirationTask(cacheOperations, instrumentationProvider)); }
public void TestInitialize() { scavengedKeys = ""; inMemoryCache = new Hashtable(); instrumentationProvider = new CachingInstrumentationProvider(); }
public static void InvokeRefreshAction(CacheItem removedCacheItem, CacheItemRemovedReason removalReason, CachingInstrumentationProvider instrumentationProvider) { if (removedCacheItem.RefreshAction == null) { return; } try { RefreshActionData refreshActionData = new RefreshActionData(removedCacheItem.RefreshAction, removedCacheItem.Key, removedCacheItem.Value, removalReason, instrumentationProvider); refreshActionData.InvokeOnThreadPoolThread(); } catch (Exception e) { instrumentationProvider.FireCacheFailed(Resources.FailureToSpawnUserSpecifiedRefreshAction, e); } }
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 }
/// <summary> /// Made protected for testing purposes. /// </summary> /// <param name="cacheOperations">For testing only.</param> /// <param name="instrumentationProvider">For testing only.</param> /// <returns>For testing only.</returns> protected internal virtual ExpirationTask CreateExpirationTask(ICacheOperations cacheOperations, CachingInstrumentationProvider instrumentationProvider) { return(new ExpirationTask(cacheOperations, instrumentationProvider)); }