Exemple #1
0
        public void SlidingExpirationRenewedByAccess()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            var result = cache.Set(key, context =>
            {
                context.SetSlidingExpiration(TimeSpan.FromSeconds(1));
                context.Data.Write(value, 0, value.Length);
            });

            Assert.Equal(value, result.ReadAllBytes());

            var found = cache.TryGetValue(key, out result);

            Assert.True(found);
            Assert.Equal(value, result.ReadAllBytes());

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

                found = cache.TryGetValue(key, out result);
                Assert.True(found);
                Assert.Equal(value, result.ReadAllBytes());
            }

            Thread.Sleep(TimeSpan.FromSeconds(3));
            found = cache.TryGetValue(key, out result);

            Assert.False(found);
            Assert.Null(result);
        }
        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(1));

            result = cache.Get(key);
            Assert.Null(result);
        }
Exemple #3
0
        public void AbsoluteExpirationExpires()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            var result = cache.Set(key, context =>
            {
                context.SetAbsoluteExpiration(DateTimeOffset.UtcNow + TimeSpan.FromSeconds(1));
                context.Data.Write(value, 0, value.Length);
            });

            Assert.Equal(value, result.ReadAllBytes());

            var found = cache.TryGetValue(key, out result);

            Assert.True(found);
            Assert.Equal(value, result.ReadAllBytes());

            for (int i = 0; i < 4 && found; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
                found = cache.TryGetValue(key, out result);
            }

            Assert.False(found);
            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 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 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 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);
        }
Exemple #8
0
        public void GetMissingKeyReturnsFalseOrNull()
        {
            var    cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            string key   = "non-existent-key";

            var result = cache.Get(key);

            Assert.Null(result);

            var found = cache.TryGetValue(key, out result);

            Assert.False(found);
        }
Exemple #9
0
        public void RemoveRemoves()
        {
            var    cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var    value = new byte[1];
            string key   = "myKey";

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

            Assert.Equal(value, result.ReadAllBytes());

            cache.Remove(key);
            result = cache.Get(key);
            Assert.Null(result);
        }
Exemple #10
0
        public void NegativeRelativeExpirationThrows()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            ExceptionAssert.ThrowsArgumentOutOfRange(() =>
            {
                var result = cache.Set(key, context =>
                {
                    context.SetAbsoluteExpiration(TimeSpan.FromMinutes(-1));
                    context.Data.Write(value, 0, value.Length);
                });
            }, "relative", "The relative expiration value must be positive.", TimeSpan.FromMinutes(-1));
        }
Exemple #11
0
        public void ZeroSlidingExpirationThrows()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            ExceptionAssert.ThrowsArgumentOutOfRange(() =>
            {
                var result = cache.Set(key, context =>
                {
                    context.SetSlidingExpiration(TimeSpan.Zero);
                    context.Data.Write(value, 0, value.Length);
                });
            }, "offset", "The sliding expiration value must be positive.", TimeSpan.Zero);
        }
        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);
        }
        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.ToString(CultureInfo.CurrentCulture));
        }
Exemple #15
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(() =>
            {
                var result = cache.Set(key, context =>
                {
                    context.SetAbsoluteExpiration(expected);
                    context.Data.Write(value, 0, value.Length);
                });
            }, "absolute", "The absolute expiration value must be in the future.", expected.ToString(CultureInfo.CurrentCulture));
        }
        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);
        }
        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);
        }
Exemple #18
0
        public void SlidingSubSecondExpirationExpiresImmediately()
        {
            var cache = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var key   = "myKey";
            var value = new byte[1];

            var result = cache.Set(key, context =>
            {
                context.SetSlidingExpiration(TimeSpan.FromSeconds(0.25));
                context.Data.Write(value, 0, value.Length);
            });

            Assert.Equal(value, result.ReadAllBytes());

            var found = cache.TryGetValue(key, out result);

            Assert.False(found);
            Assert.Null(result);
        }
Exemple #19
0
        public void GetOrSetDoesNotOverwrite()
        {
            var cache  = RedisTestConfig.CreateCacheInstance(GetType().Name);
            var value1 = new byte[1] {
                1
            };
            var value2 = new byte[1] {
                2
            };
            string key = "myKey";

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

            Assert.Equal(value1, result.ReadAllBytes());

            // Retrieved
            result = cache.GetOrSet(key, value2);
            Assert.Equal(value1, result.ReadAllBytes());
        }
        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);
        }