Exemplo n.º 1
0
        public void LeastRecentlyUsedCacheInsertThrowsIfKeyIsNull()
        {
            var cache     = new LeastRecentlyUsedCache <int?, TestCacheStub>(5);
            var cacheItem = new TestCacheStub {
                Id = 1, Value = 1.1
            };

            Assert.Throws <ArgumentNullException>(() => cache.Insert(null, cacheItem));
        }
Exemplo n.º 2
0
        public void LeastRecentlyUsedCacheInsertsItemIntoCache()
        {
            const int expectedCount = 1;

            var cache     = new LeastRecentlyUsedCache <int, TestCacheStub>(5);
            var cacheItem = new TestCacheStub {
                Id = 1, Value = 1.1
            };

            cache.Insert(cacheItem.Id, cacheItem);

            Assert.AreEqual(expectedCount, cache.Count);
        }
Exemplo n.º 3
0
        public void LeastRecentlyUsedCacheTryGetItemReturnsFalseIfItemIsInCache()
        {
            var cache     = new LeastRecentlyUsedCache <int, TestCacheStub>(5);
            var cacheItem = new TestCacheStub {
                Id = 1, Value = 1.1
            };

            cache.Insert(cacheItem.Id, cacheItem);

            TestCacheStub value;
            bool          result = cache.TryGetItem(2, out value);

            Assert.IsFalse(result);
        }
        public void Can_get_item_from_cache_and_expires_absolute()
        {
            // Arrange
            const string cacheKey = "TestAbsoluteKey";
            NetMemoryCacheManager memoryCacheManager = new NetMemoryCacheManager {AbsoluteExpiry = 2};

            // Act
            TestCacheStub testCacheClass = new TestCacheStub();

            memoryCacheManager.AddOrUpdate(cacheKey, testCacheClass);

            // Assert
            Assert.AreEqual(testCacheClass, memoryCacheManager.Get<TestCacheStub>(cacheKey));
            Thread.Sleep(new TimeSpan(0,0,0,1));
            Assert.AreEqual(testCacheClass, memoryCacheManager.Get<TestCacheStub>(cacheKey));
            Thread.Sleep(new TimeSpan(0, 0, 0, 1));
            Assert.IsNull(memoryCacheManager.Get<TestCacheStub>(cacheKey));
        }
        public void Cache_destroyed_and_recreated()
        {
            // Arrange
            const string cacheKey = "TestDestroyedCache";
            NetMemoryCacheManager memoryCacheManager = new NetMemoryCacheManager {AbsoluteExpiry = 200};

            // Act
            TestCacheStub testCacheClass = new TestCacheStub();
            memoryCacheManager.AddOrUpdate(cacheKey, testCacheClass);

            // Assert
            Assert.AreEqual(testCacheClass, memoryCacheManager.Get<TestCacheStub>(cacheKey));

            memoryCacheManager.ClearCache();
            Assert.IsNull(memoryCacheManager.Get<TestCacheStub>(cacheKey));

            memoryCacheManager.AddOrUpdate(cacheKey, testCacheClass);
            Assert.AreEqual(testCacheClass, memoryCacheManager.Get<TestCacheStub>(cacheKey));
        }
        public void Can_get_item_from_cache_and_expires_absolute()
        {
            // Arrange
            const string          cacheKey           = "TestAbsoluteKey";
            NetMemoryCacheManager memoryCacheManager = new NetMemoryCacheManager {
                AbsoluteExpiry = 2
            };

            // Act
            TestCacheStub testCacheClass = new TestCacheStub();

            memoryCacheManager.AddOrUpdate(cacheKey, testCacheClass);

            // Assert
            Assert.AreEqual(testCacheClass, memoryCacheManager.Get <TestCacheStub>(cacheKey));
            Thread.Sleep(new TimeSpan(0, 0, 0, 1));
            Assert.AreEqual(testCacheClass, memoryCacheManager.Get <TestCacheStub>(cacheKey));
            Thread.Sleep(new TimeSpan(0, 0, 0, 1));
            Assert.IsNull(memoryCacheManager.Get <TestCacheStub>(cacheKey));
        }
        public void Cache_destroyed_and_recreated()
        {
            // Arrange
            const string          cacheKey           = "TestDestroyedCache";
            NetMemoryCacheManager memoryCacheManager = new NetMemoryCacheManager {
                AbsoluteExpiry = 200
            };

            // Act
            TestCacheStub testCacheClass = new TestCacheStub();

            memoryCacheManager.AddOrUpdate(cacheKey, testCacheClass);

            // Assert
            Assert.AreEqual(testCacheClass, memoryCacheManager.Get <TestCacheStub>(cacheKey));

            memoryCacheManager.ClearCache();
            Assert.IsNull(memoryCacheManager.Get <TestCacheStub>(cacheKey));

            memoryCacheManager.AddOrUpdate(cacheKey, testCacheClass);
            Assert.AreEqual(testCacheClass, memoryCacheManager.Get <TestCacheStub>(cacheKey));
        }