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 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 SetAndGetReturnsObject() { 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()); result = cache.Get(key); Assert.Equal(value, result.ReadAllBytes()); }
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); }
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); }
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)); }
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 SetAndGetWorksWithCaseSensitiveKeys() { var cache = RedisTestConfig.CreateCacheInstance(GetType().Name); var value = new byte[1]; string key1 = "myKey"; string key2 = "Mykey"; var result = cache.Set(key1, value); Assert.Equal(value, result.ReadAllBytes()); result = cache.Get(key1); Assert.Equal(value, result.ReadAllBytes()); 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(() => { 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 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); }
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"; var result = cache.Set(key, value1); Assert.Equal(value1, result.ReadAllBytes()); var value2 = new byte[1] { 2 }; result = cache.Set(key, value2); Assert.Equal(value2, result.ReadAllBytes()); result = cache.Get(key); Assert.Equal(value2, result.ReadAllBytes()); }