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();
        }
示例#4
0
 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 TestInitialize()
		{
			inMemoryCache = new Hashtable();
			instrumentationProvider = new CachingInstrumentationProvider();
			expirer = new ExpirationTask(this, instrumentationProvider);
			expiredItemKeys = "";
			callbackCount = 0;
			callbackReason = CacheItemRemovedReason.Unknown;
		}
示例#6
0
        /// <summary>
        /// Initialize a new instance of the <see cref="ScavengerTask"/> with a <see cref="CacheManager"/> name, a <see cref="CachingConfigurationView"/>, the <see cref="CacheCapacityScavengingPolicy"/> and the <see cref="ICacheOperations"/>.
        /// </summary>
        /// <param name="numberToRemoveWhenScavenging">The number of items that should be removed from the cache when scavenging.</param>
        /// <param name="scavengingPolicy">The <see cref="CacheCapacityScavengingPolicy"/> to use.</param>
        /// <param name="cacheOperations">The <see cref="ICacheOperations"/> to perform.</param>
        /// <param name="instrumentationProvider">An instrumentation provider.</param>
        public ScavengerTask(int numberToRemoveWhenScavenging,
							   CacheCapacityScavengingPolicy scavengingPolicy,
							   ICacheOperations cacheOperations,
							   CachingInstrumentationProvider instrumentationProvider)
        {
            this.numberToRemoveWhenScavenging = numberToRemoveWhenScavenging;
            this.scavengingPolicy = scavengingPolicy;
            this.cacheOperations = cacheOperations;
            this.instrumentationProvider = instrumentationProvider;
        }
		/// <summary>
		/// Initialize a new instance of the <see cref="BackgroundScheduler"/> with a <see cref="ExpirationTask"/> and a <see cref="ScavengerTask"/>.
		/// </summary>
		/// <param name="expirer">The expiration task to use.</param>
		/// <param name="scavenger">The scavenger task to use.</param>
		/// <param name="instrumentationProvider">The instrumentation provider to use.</param>
        public BackgroundScheduler(ExpirationTask expirer, ScavengerTask scavenger, CachingInstrumentationProvider instrumentationProvider)
        {
            this.expirer = expirer;
            this.scavenger = scavenger;
			this.instrumentationProvider = instrumentationProvider;

            ThreadStart queueReader = new ThreadStart(QueueReader);
            inputQueueThread = new Thread(queueReader);
            inputQueueThread.IsBackground = true;
        }
示例#8
0
		/// <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);
        }
示例#9
0
        /// <summary>
        /// Invokes the refresh action on a thread pool thread
        /// </summary>
        /// <param name="removedCacheItem">Cache item being removed. Must never be null.</param>
        /// <param name="removalReason">The reason the item was removed.</param>	
        /// <param name="instrumentationProvider">The instrumentation provider.</param>
        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 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"));
                }
            }
        }
            protected internal override ExpirationTask CreateExpirationTask(ICacheOperations cacheOperations, CachingInstrumentationProvider instrumentationProvider)
            {
				return new RaceConditionSimulatingExpirationTask(cacheOperations, instrumentationProvider);
            }
            public RaceConditionSimulatingExpirationTask(ICacheOperations cacheOperations, CachingInstrumentationProvider instrumentationProvider) 
				: base(cacheOperations, instrumentationProvider)
            {
            }
 /// <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);
 }
示例#15
0
		public void TestInitialize()
		{
			scavengedKeys = "";
			inMemoryCache = new Hashtable();
			instrumentationProvider = new CachingInstrumentationProvider();
		}
        /// <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);
        }
示例#17
0
 /// <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;
 }