Пример #1
0
        public void SlidingExpirationRenewedByAccessUntilAbsoluteExpiration()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            cache.Set(key, value, new DistributedCacheEntryOptions()
                      .SetSlidingExpiration(TimeSpan.FromSeconds(1))
                      .SetAbsoluteExpiration(TimeSpan.FromSeconds(3)));

            var result = cache.Get(key);

            Assert.Equal(value, result);

            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));

                result = cache.Get(key);
                Assert.Equal(value, result);
            }

            Thread.Sleep(TimeSpan.FromSeconds(.6));

            result = cache.Get(key);
            Assert.Null(result);
        }
        public void GetMissingKeyReturnsNull()
        {
            var    cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            string key   = "non-existent-key";

            var result = cache.Get(key);

            Assert.Null(result);
        }
        public void SetNullValueThrows()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);

            byte[] value = null;
            string key   = "myKey";

            Assert.Throws <ArgumentNullException>(() => cache.Set(key, value));
        }
Пример #4
0
        public void NegativeSlidingExpirationThrows()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            ExceptionAssert.ThrowsArgumentOutOfRange(() =>
            {
                cache.Set(key, value, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(-1)));
            }, nameof(DistributedCacheEntryOptions.SlidingExpiration), "The sliding expiration value must be positive.", TimeSpan.FromMinutes(-1));
        }
        public void SetAndGetReturnsObject()
        {
            var    cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var    value = new byte[1];
            string key   = "myKey";

            cache.Set(key, value);

            var result = cache.Get(key);

            Assert.Equal(value, result);
        }
Пример #6
0
        public void AbsoluteSubSecondExpirationExpiresImmidately()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            cache.Set(key, value, new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(0.25)));

            var result = cache.Get(key);

            Assert.Null(result);
        }
        public void RemoveRemoves()
        {
            var    cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var    value = new byte[1];
            string key   = "myKey";

            cache.Set(key, value);
            var result = cache.Get(key);

            Assert.Equal(value, result);

            cache.Remove(key);
            result = cache.Get(key);
            Assert.Null(result);
        }
Пример #8
0
        public void ZeroRelativeExpirationThrows()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            ExceptionAssert.ThrowsArgumentOutOfRange(
                () =>
            {
                cache.Set(key, value, new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.Zero));
            },
                nameof(DistributedCacheEntryOptions.AbsoluteExpirationRelativeToNow),
                "The relative expiration value must be positive.",
                TimeSpan.Zero);
        }
        public void SetAndGetWorksWithCaseSensitiveKeys()
        {
            var    cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var    value = new byte[1];
            string key1  = "myKey";
            string key2  = "Mykey";

            cache.Set(key1, value);

            var result = cache.Get(key1);

            Assert.Equal(value, result);

            result = cache.Get(key2);
            Assert.Null(result);
        }
Пример #10
0
        public void AbsoluteExpirationInThePastThrows()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            var expected = DateTimeOffset.Now - TimeSpan.FromMinutes(1);

            ExceptionAssert.ThrowsArgumentOutOfRange(
                () =>
            {
                cache.Set(key, value, new DistributedCacheEntryOptions().SetAbsoluteExpiration(expected));
            },
                nameof(DistributedCacheEntryOptions.AbsoluteExpiration),
                "The absolute expiration value must be in the future.",
                expected);
        }
Пример #11
0
        public void SlidingExpirationExpiresIfNotAccessed()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            cache.Set(key, value, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(1)));

            var result = cache.Get(key);

            Assert.Equal(value, result);

            Thread.Sleep(TimeSpan.FromSeconds(3));

            result = cache.Get(key);
            Assert.Null(result);
        }
Пример #12
0
        public void AbsoluteExpirationExpires()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            cache.Set(key, value, new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(1)));

            byte[] result = cache.Get(key);
            Assert.Equal(value, result);

            for (int i = 0; i < 4 && (result != null); i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                result = cache.Get(key);
            }

            Assert.Null(result);
        }
        public void SetAlwaysOverwrites()
        {
            var cache  = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var value1 = new byte[1] {
                1
            };
            string key = "myKey";

            cache.Set(key, value1);
            var result = cache.Get(key);

            Assert.Equal(value1, result);

            var value2 = new byte[1] {
                2
            };

            cache.Set(key, value2);
            result = cache.Get(key);
            Assert.Equal(value2, result);
        }