Example #1
0
        public void TestRedisCacheTimeToLive_FromInsert()
        {
            var           ttl    = 10;
            int           misses = 0;
            string        result;
            var           stopwatch = new Stopwatch();
            Func <string> getter    = () => { misses++; return(misses.ToString()); };

            var cache = new RedisCache(
                "insert-expire-cache",
                new RedisCachePolicy
            {
                ExpirationFromAdd = TimeSpan.FromSeconds(ttl),
            });

            cache.ClearAll();

            stopwatch.Start();
            result = cache.Get("key", getter);
            Assert.AreEqual(1, misses);
            Assert.AreEqual("1", result);

            result = cache.Get("key", getter);
            Assert.AreEqual(1, misses);
            Assert.AreEqual("1", result);

            CacheTestTools.AssertValueDoesntChangeWithin(cache, "key", "1", getter, stopwatch, ttl - 1);

            // Should expire within TTL+60sec from insert
            CacheTestTools.AssertValueDoesChangeWithin(cache, "key", "1", getter, stopwatch, 60.1);

            result = cache.Get("key", getter);
            Assert.AreNotEqual(1, misses);
            Assert.AreNotEqual("1", result);
        }
Example #2
0
        public void TestRedisCacheTimeToLive_Constant()
        {
            var           ttl    = 10;
            int           misses = 0;
            string        result;
            var           stopwatch = new Stopwatch();
            Func <string> getter    = () => { misses++; return(misses.ToString()); };

            var expireAt = DateTime.Now.AddSeconds(ttl);

            stopwatch.Start();

            var cache = new RedisCache(
                "constant-expire",
                new RedisCachePolicy
            {
                ConnectionString   = connectionString,
                AbsoluteExpiration = expireAt,
            });

            cache.ClearAll();

            result = cache.Get("key", getter);
            DateTime insertTime = DateTime.Now;

            Assert.AreEqual(1, misses);
            Assert.AreEqual("1", result);

            result = cache.Get("key", getter);
            Assert.AreEqual(1, misses);
            Assert.AreEqual("1", result);

            CacheTestTools.AssertValueDoesntChangeWithin(cache, "key", "1", getter, stopwatch, ttl - 1);

            // Should expire within TTL+60sec from insert
            CacheTestTools.AssertValueDoesChangeWithin(cache, "key", "1", getter, stopwatch, 60.1);

            result = cache.Get("key", getter);
            Assert.AreNotEqual(1, misses);
            Assert.AreNotEqual("1", result);
        }
Example #3
0
        public void TestRedisCacheTimeToLive_Sliding()
        {
            var           ttl    = 10;
            int           misses = 0;
            string        result;
            var           stopwatch = new Stopwatch();
            Func <string> getter    = () => { misses++; return(misses.ToString()); };

            var cache = new RedisCache(
                "sliding-expire-cache",
                new RedisCachePolicy
            {
                ConnectionString  = connectionString,
                SlidingExpiration = TimeSpan.FromSeconds(ttl),
            });

            cache.ClearAll();

            stopwatch.Start();
            result = cache.Get("key", getter);
            DateTime insertTime = DateTime.Now;

            Assert.AreEqual(1, misses);
            Assert.AreEqual("1", result);

            result = cache.Get("key", getter);
            Assert.AreEqual(1, misses);
            Assert.AreEqual("1", result);

            CacheTestTools.AssertValueDoesntChangeWithin(cache, "key", "1", getter, stopwatch, ttl - 1 + 60);

            // Should expire within TTL+60sec from last access
            CacheTestTools.AssertValueDoesChangeAfter(cache, "key", "1", getter, stopwatch, ttl + 60.1);

            result = cache.Get("key", getter);
            Assert.AreNotEqual(1, misses);
            Assert.AreNotEqual("1", result);
        }