Beispiel #1
0
        public void Test_Sliding()
        {
            MockTimeProvider time = new MockTimeProvider();

            time.Now = new DateTime(2000, 1, 1);

            SmartCache <int> cache = new SmartCache <int>(5, time);

            int item;

            cache.Add("1", 1, TimeSpan.FromMinutes(100));
            cache.Add("2", 2, TimeSpan.FromMinutes(200));
            cache.Add("3", 3, TimeSpan.FromMinutes(300));

            time.Now += TimeSpan.FromMinutes(20);

            Assert.IsTrue(cache.TryGetValue("1", out item));
            Assert.AreEqual(3, cache.ItemCount);

            time.Now += TimeSpan.FromMinutes(150);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, cache.ItemCount);

            time.Now += TimeSpan.FromDays(1);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsFalse(cache.TryGetValue("3", out item));

            Assert.AreEqual(0, cache.ItemCount);
        }
        public void Test_Expiring()
        {
            MockTimeProvider time = new MockTimeProvider();

            time.Now = new DateTime(2000, 1, 1);

            SmartCache<int> cache = new SmartCache<int>(5, time);

            int item;

            cache.Add("1", 1, time.Now.AddMinutes(100));
            cache.Add("2", 2, time.Now.AddMinutes(200));
            cache.Add("3", 3, time.Now.AddMinutes(300));

            Assert.IsTrue(cache.TryGetValue("1", out item));
            Assert.AreEqual(3, cache.ItemCount);

            time.Now += TimeSpan.FromMinutes(150);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.AreEqual(2, cache.ItemCount);

            time.Now += TimeSpan.FromMinutes(100);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.AreEqual(1, cache.ItemCount);

            time.Now += TimeSpan.FromMinutes(100);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.AreEqual(0, cache.ItemCount);
        }