public void TestCustomExpiration()
        {
            ExpirableCacheStore expirableCacheStore = new ExpirableCacheStore(new CacheOptions());

            CacheItem cacheItem = new CacheItem("cacheItem", null, 0, new IExpirationSpecifier[] { new TestExpirationSpecifier() });
            expirableCacheStore.Set(cacheItem);

            Assert.IsTrue(Object.ReferenceEquals(cacheItem, expirableCacheStore.Get(cacheItem.Key, false)), "unable to get object");

            ((TestExpirationSpecifier)cacheItem.ExpirationSpecifierList[0]).Expired = true;
            Assert.IsNull(expirableCacheStore.Get(cacheItem.Key, false), "unable to expire object");
        }
        public void TestAbsoluteExpiration()
        {
            ExpirableCacheStore expirableCacheStore = new ExpirableCacheStore(new CacheOptions());

            CacheItem cacheItem = new CacheItem("cacheItem", null, 0, new IExpirationSpecifier[] { new AbsoluteTimeExpirationSpecifier(DateTime.UtcNow.AddSeconds(1)) });
            expirableCacheStore.Set(cacheItem);

            Assert.IsTrue(Object.ReferenceEquals(cacheItem, expirableCacheStore.Get(cacheItem.Key, false)), "unable to get object");
            Thread.Sleep(300);
            Assert.IsTrue(Object.ReferenceEquals(cacheItem, expirableCacheStore.Get(cacheItem.Key, false)), "unable to get object");
            Thread.Sleep(900);
            Assert.IsNull(expirableCacheStore.Get(cacheItem.Key, false), "unable to expire object");
        }
Exemplo n.º 3
0
        public void TestSetGetMultiThreaded()
        {
            CacheStore cacheStore = new CacheStore(new CacheOptions());

            ParallelLoopResult result = Parallel.For(1, 100000, delegate(int ndx)
            {
                string key = String.Format("cacheItem{0}", ndx);
                CacheItem cacheItem = new CacheItem(key, null, 0, null);
                cacheStore.Set(cacheItem);
                Assert.IsTrue(Object.ReferenceEquals(cacheItem, cacheStore.Get(cacheItem.Key)), "unable to read back value from ICacheStore");
            });

            Assert.IsTrue(result.IsCompleted, "failed multi threaded Set/Get");
        }
        public void TestSlidingExpiration()
        {
            ExpirableCacheStore expirableCacheStore = new ExpirableCacheStore(new CacheOptions());

            CacheItem cacheItem = new CacheItem("cacheItem", null, 0, new IExpirationSpecifier[] { new SlidingWindowExpirationSpecifier(TimeSpan.FromSeconds(1)) });
            expirableCacheStore.Set(cacheItem);

            Assert.IsTrue(Object.ReferenceEquals(cacheItem, expirableCacheStore.Get(cacheItem.Key, false)), "unable to get object");
            Thread.Sleep(300);
            Assert.IsTrue(Object.ReferenceEquals(cacheItem, expirableCacheStore.Get(cacheItem.Key, false)), "unable to get object");
            Thread.Sleep(900);
            Assert.IsTrue(Object.ReferenceEquals(cacheItem, expirableCacheStore.Get(cacheItem.Key, false)), "unable to get object");
            Thread.Sleep(1100);
            Assert.IsNull(expirableCacheStore.Get(cacheItem.Key, false), "unable to expire object");
        }
        public void TestExpirationPolling()
        {
            ExpirableCacheStore expirableCacheStore = new ExpirableCacheStore(new CacheOptions { ExpirationPollInterval = TimeSpan.FromSeconds(1) });

            CacheItem cacheItem = new CacheItem("cacheItem", null, 0, new IExpirationSpecifier[] { new TestExpirationSpecifier() });
            expirableCacheStore.Set(cacheItem);

            Assert.AreEqual(expirableCacheStore.CacheItemCount, 1, "count mismatch");

            Thread.Sleep(1100);
            Assert.AreEqual(expirableCacheStore.CacheItemCount, 1, "count mismatch");

            ((TestExpirationSpecifier)cacheItem.ExpirationSpecifierList[0]).Expired = true;
            Thread.Sleep(1100);
            Assert.AreEqual(expirableCacheStore.CacheItemCount, 0, "count mismatch");
        }
Exemplo n.º 6
0
        public void TestGetKeys()
        {
            CacheStore cacheStore = new CacheStore(new CacheOptions());

            CacheItem cacheItem1 = new CacheItem("cacheItem1", "cacheItem1", 0, null);
            CacheItem cacheItem2 = new CacheItem("cacheItem2", "cacheItem2", 0, null);

            cacheStore.Set(cacheItem1);
            cacheStore.Set(cacheItem2);

            Assert.AreEqual(cacheStore.CacheItemCount, 2, "cache item count mismatch");

            var keyList = cacheStore.GetKeys().ToList();

            Assert.AreEqual(keyList.Count, 2, "unexpected number of keys");

            Assert.IsTrue(keyList.Contains(cacheItem1.Key), "key not found");
            Assert.IsTrue(keyList.Contains(cacheItem2.Key), "key not found");
        }
Exemplo n.º 7
0
        public void TestSetGetRemove()
        {
            CacheStore cacheStore = new CacheStore(new CacheOptions());

            CacheItem cacheItem1 = new CacheItem("cacheItem1", null, 0, null);
            CacheItem cacheItem2 = new CacheItem("cacheItem2", null, 0, null);

            cacheStore.Set(cacheItem1);
            cacheStore.Set(cacheItem2);

            Assert.IsTrue(Object.ReferenceEquals(cacheItem1, cacheStore.Get("cacheItem1")), "unable to read back value from ICacheStore");
            Assert.IsTrue(Object.ReferenceEquals(cacheItem2, cacheStore.Get("cacheItem2")), "unable to read back value from ICacheStore");

            cacheStore.Remove(cacheItem1.Key);
            Assert.IsNull(cacheStore.Get(cacheItem1.Key), "failed to remove cache item");
            Assert.IsTrue(Object.ReferenceEquals(cacheItem2, cacheStore.Get(cacheItem2.Key)), "unable to read back value from ICacheStore");

            cacheStore.Remove(cacheItem2.Key);
            Assert.IsNull(cacheStore.Get(cacheItem2.Key), "failed to remove cache item");
        }
        public void TestPriorityBasedScavenging()
        {
            const int MaxCacheItemCount = 1000;
            const decimal ScavengePercentage = 50;
            const int MaxItemsAfterScavenging = (int)(MaxCacheItemCount * ScavengePercentage / 100);
            const int NumberOfItemsToInsert = MaxCacheItemCount + 1;

            ScavengedExpirableCacheStore cacheStore = new ScavengedExpirableCacheStore(new CacheOptions { ExpirationPollInterval = TimeSpan.FromSeconds(1), MaxCacheItemCount = MaxCacheItemCount, ScavengePercentage = ScavengePercentage });

            // insert with increasing priority
            for (int i = 0; i < NumberOfItemsToInsert; i++)
            {
                CacheItem cacheItem = new CacheItem(String.Format("key{0}", i), null, i + 1, null);
                cacheStore.Set(cacheItem);
            }

            Thread.Sleep(3000);

            int cacheItemCount = cacheStore.CacheItemCount;
            Assert.IsTrue(cacheItemCount == MaxItemsAfterScavenging, "scavenging did not occur as expected");

            for (int i = 0; i < cacheItemCount; i++)
            {
                CacheItem cacheItem = cacheStore.Get(String.Format("key{0}", i), false);
                Assert.IsNotNull(cacheItem, "cache item is expected to be present");
            }

            for (int i = cacheItemCount; i < NumberOfItemsToInsert; i++)
            {
                CacheItem cacheItem = cacheStore.Get(String.Format("key{0}", i), false);
                Assert.IsNull(cacheItem, "cache item is expected to be absent");
            }

            // insert with max priority
            for (int i = cacheItemCount; i < NumberOfItemsToInsert; i++)
            {
                CacheItem cacheItem = new CacheItem(String.Format("key{0}", i), null, 0, null);
                cacheStore.Set(cacheItem);
            }

            _ReflectivelyTriggerScavenging(cacheStore);

            cacheItemCount = cacheStore.CacheItemCount;
            Assert.IsTrue(cacheItemCount == MaxItemsAfterScavenging, "scavenging did not occur as expected");

            int numMissing = 0;
            for (int i = NumberOfItemsToInsert - 1; i >= NumberOfItemsToInsert - cacheItemCount; i--)
            {
                CacheItem cacheItem = cacheStore.Get(String.Format("key{0}", i), false);
                numMissing += (cacheItem != null ? 0 : 1);
                Assert.IsTrue(numMissing <= 1, "more than expected number of items missing");
            }

            int numFound = 0;
            for (int i = NumberOfItemsToInsert - cacheItemCount - 1; i >= 0; i--)
            {
                CacheItem cacheItem = cacheStore.Get(String.Format("key{0}", i), false);
                numFound += (cacheItem != null ? 1 : 0);
                Assert.IsTrue(numFound <= 1, "more than expected number of items found");
            }
        }
        public void TestScavengingWithExpiry()
        {
            const int MaxCacheItemCount = 1000;
            const decimal ScavengePercentage = 50;
            const int MaxItemsAfterScavenging = (int)(MaxCacheItemCount * ScavengePercentage / 100);
            const int NumberOfItemsToInsert = MaxCacheItemCount + 1;

            ScavengedExpirableCacheStore cacheStore = new ScavengedExpirableCacheStore(new CacheOptions { ExpirationPollInterval = TimeSpan.FromMinutes(2), MaxCacheItemCount = MaxCacheItemCount, ScavengePercentage = ScavengePercentage });

            TestExpirationSpecifier testExpirationSpecifier = new TestExpirationSpecifier();

            for (int i = 0; i < NumberOfItemsToInsert; i++)
            {
                IExpirationSpecifier[] expirationSpecifiers = i >= MaxItemsAfterScavenging ? new IExpirationSpecifier[] { testExpirationSpecifier } : null;
                CacheItem cacheItem = new CacheItem(String.Format("key{0}", i), null, 0, expirationSpecifiers);
                cacheStore.Set(cacheItem);
            }

            testExpirationSpecifier.Expired = true;
            Thread.Sleep(3000);

            int cacheItemCount = cacheStore.CacheItemCount;
            Assert.IsTrue(cacheItemCount == MaxItemsAfterScavenging, "scavenging improper");

            for (int i = 0; i < cacheItemCount; i++)
            {
                CacheItem cacheItem = cacheStore.Get(String.Format("key{0}", i), false);
                Assert.IsNotNull(cacheItem, "cacheItem expected");
            }
        }