Esempio n. 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);
        }
Esempio n. 2
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);
        }