void Put_sets_an_expiration_on_the_item() { var config = new RedisCacheConfiguration("region") { Expiration = TimeSpan.FromSeconds(30) }; var sut = new RedisCache(config, ConnectionMultiplexer, options); sut.Put(999, new Person("Foo", 10)); var cacheKey = sut.CacheNamespace.GetKey(999); var ttl = Redis.KeyTimeToLive(cacheKey); Assert.InRange(ttl.Value, TimeSpan.FromSeconds(29), TimeSpan.FromSeconds(30)); }
void Configure_cache_expiration() { var configuration = new RedisCacheConfiguration("region") { Expiration = TimeSpan.FromMinutes(99) }; var sut = new RedisCache(configuration, ConnectionMultiplexer, options); sut.Put(999, new Person("Foo", 10)); var cacheKey = sut.CacheNamespace.GetKey(999); var expiry = Redis.KeyTimeToLive(cacheKey); Assert.InRange(expiry.Value, low: TimeSpan.FromMinutes(98), high: TimeSpan.FromMinutes(99)); }
void Configure_cache_lock_timeout() { var configuration = new RedisCacheConfiguration("region") { LockTimeout = TimeSpan.FromSeconds(123) }; var sut = new RedisCache(configuration, ConnectionMultiplexer, options); const string key = "123"; sut.Lock(key); var lockKey = sut.CacheNamespace.GetLockKey(key); var expiry = Redis.KeyTimeToLive(lockKey); Assert.InRange(expiry.Value, low: TimeSpan.FromSeconds(120), high: TimeSpan.FromSeconds(123)); }
void Get_when_sliding_expiration_set_and_time_to_live_is_greater_than_expiration_does_not_reset_the_expiration() { var config = new RedisCacheConfiguration("region") { Expiration = TimeSpan.FromMilliseconds(500), SlidingExpiration = TimeSpan.FromMilliseconds(100) }; var sut = new RedisCache(config, ConnectionMultiplexer, options); sut.Put(1, new Person("John Doe", 10)); Thread.Sleep(TimeSpan.FromMilliseconds(200)); var result = sut.Get(1); var cacheKey = sut.CacheNamespace.GetKey(1); var expiry = Redis.KeyTimeToLive(cacheKey); Assert.InRange(expiry.Value, TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(300)); }
public void Create_ReturnsRedisCache() { RedisCacheConfiguration config = new RedisCacheConfiguration() { Host = "localhost", Port = 1234, Password = "******", InstanceId = "instanceId" }; RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword"); si.ApplicationInfo = new ApplicationInstanceInfo() { InstanceId = "instanceId" }; var factory = new RedisServiceConnectorFactory(si, config); var cache = factory.Create(null); Assert.NotNull(cache); }
public void Configure_NoServiceInfo_ReturnsExpected() { RedisCacheConfigurer configurer = new RedisCacheConfigurer(); RedisCacheConfiguration config = new RedisCacheConfiguration() { Host = "localhost", Port = 1234, Password = "******", InstanceId = "instanceId" }; var opts = configurer.Configure(null, config); Assert.NotNull(opts); var redisOptions = opts.Value; Assert.NotNull(redisOptions); Assert.Equal("localhost:1234,password=password", redisOptions.Configuration); Assert.Equal("instanceId", redisOptions.InstanceName); }
void Get_after_item_has_expired_removes_the_key_from_set_of_all_keys() { const int key = 1; var config = new RedisCacheConfiguration("region") { Expiration = TimeSpan.FromMilliseconds(500), SlidingExpiration = RedisCacheConfiguration.NoSlidingExpiration }; var sut = new RedisCache(config, ConnectionMultiplexer, options); sut.Put(key, new Person("John Doe", 20)); Thread.Sleep(TimeSpan.FromMilliseconds(600)); var result = sut.Get(key); var setOfActiveKeysKey = sut.CacheNamespace.GetSetOfActiveKeysKey(); var cacheKey = sut.CacheNamespace.GetKey(key); var isKeyStillTracked = Redis.SetContains(setOfActiveKeysKey, cacheKey); Assert.False(isKeyStillTracked); }
public RystemCacheServiceProvider WithRedisCache(RedisCacheConfiguration configuration = default, string serviceKey = default) => (RystemCacheServiceProvider)WithIntegration(ServiceProviderType.AzureRedisCache, configuration, serviceKey);
void Get_when_sliding_expiration_and_time_to_live_is_less_than_expiration_resets_the_expiration() { var config = new RedisCacheConfiguration("region") { Expiration = TimeSpan.FromMilliseconds(500), SlidingExpiration = TimeSpan.FromMilliseconds(400) }; var sut = new RedisCache(config, ConnectionMultiplexer, options); sut.Put(1, new Person("John Doe", 10)); Thread.Sleep(TimeSpan.FromMilliseconds(200)); var result = sut.Get(1); var cacheKey = sut.CacheNamespace.GetKey(1); var expiry = Redis.KeyTimeToLive(cacheKey); Assert.InRange(expiry.Value, TimeSpan.FromMilliseconds(480), TimeSpan.FromMilliseconds(500)); }
void Get_after_item_has_expired_returns_null() { var config = new RedisCacheConfiguration("region") { Expiration = TimeSpan.FromMilliseconds(500) }; var sut = new RedisCache(config, ConnectionMultiplexer, options); sut.Put(1, new Person("John Doe", 20)); Thread.Sleep(TimeSpan.FromMilliseconds(600)); var result = sut.Get(1); Assert.Null(result); }