public void RemoveByTag_ManyItemSingleTag_ReturnsNoValues() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key1 = "TagCacheTests:Add1"; string key2 = "TagCacheTests:Add2"; string key3 = "TagCacheTests:Add3"; String value1 = "Hello World!"; String value3 = "Two"; String value2 = "Three"; DateTime expires = new DateTime(2099, 12, 11); var tags = new List <string> { "another tag" }; cache.Set(key1, value1, expires, tags); cache.Set(key2, value2, expires, tags); cache.Set(key3, value3, expires, tags); var results = cache.GetByTag <String>(tags[0]); Assert.IsNotNull(results); Assert.AreEqual(results.Count, 3); cache.RemoveByTag(tags[0]); results = cache.GetByTag <String>(tags[0]); Assert.IsNull(results); }
public void Ctor_Configuration__Succeeds() { var redis = new RedisConnectionManager("localhost"); var config = NewCacheConfiguration(redis); config.RootNameSpace = "_TestRootNamespace"; config.Serializer = new BinarySerializationProvider(); config.RedisClientConfiguration = new RedisClientConfiguration(config.RedisClientConfiguration.RedisConnectionManagerConnectionManager) { Host = _redisHost, DbNo = _redisDB, TimeoutMilliseconds = 50 }; var cache = new RedisCacheProvider(config); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); // no exception }
public void Get_AddedObject_ReturnsValue() { var redis = new RedisConnectionManager(); var configuration = GetCacheConfiguration(redis); var cache = new RedisCacheProvider(configuration); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; var value = new TestObject() { Foo = "Hello", Bar = "World", Score = 11 }; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); var result = cache.Get <TestObject>(key); Assert.IsNotNull(result); Assert.AreEqual(value.Foo, result.Foo); Assert.AreEqual(value.Bar, result.Bar); Assert.AreEqual(value.Score, result.Score); }
public static void UseRedisProvider() { var cacheProvider = new RedisCacheProvider(); cacheProvider.Set("k-name", "Redis Client"); Console.WriteLine(cacheProvider.Get <string>("k-name")); }
public void ItemExpires_RemovedFromCache() { var redis = new RedisConnectionManager(); // this is just testing the built in expiry var config = new CacheConfiguration(redis); var cache = new RedisCacheProvider(config); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:ItemExpires_RemovedFromCache"; String value = "Hello World!"; DateTime expires = DateTime.Now.AddSeconds(3); string tag1 = "tag1"; string tag2 = "tag2"; cache.Set(key, value, expires, new List<string> { tag1, tag2 }); var result = cache.Get<String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); Thread.Sleep(1000); result = cache.Get<String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); Thread.Sleep(2500); result = cache.Get<String>(key); Assert.IsNull(result); }
public void Set_String_Succeeds() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); // no exception }
public void Set_String_Succeeds() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); // no exception }
public ActionResult Index() { if (Logger == null) { Logger = new ListLogger(); } var stopwatch = new Stopwatch(); var result = new MyViewModel(); stopwatch.Start(); var cache = new RedisCacheProvider(RedisConnection); cache.Logger = Logger; stopwatch.Stop(); result.SetupTimeMs = stopwatch.ElapsedMilliseconds; stopwatch.Reset(); stopwatch.Start(); var item = cache.Get <Models.SampleModel>("models:sample1"); if (item == null) { item = new SampleModel() { DateOfBirth = DateTime.Now, Name = "Hello", Surname = "World", Other = new List <OtherModels>() { new OtherModels() { Foo = 1, Bar = 20.928 }, new OtherModels() { Foo = 31, Bar = 30.3328 }, } }; cache.Set("models:sample1", item, DateTime.Now.AddSeconds(5), new List <string>() { "Test1", "Test2" }); } stopwatch.Stop(); result.LoadTimeMs = stopwatch.ElapsedMilliseconds; result.Data = item; return(View(result)); }
public void Get_ExpiredDate_RemovesFromCache() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2000, 12, 11); cache.Set(key, value, expires); var result = cache.Get <String>(key); Assert.IsNull(result); }
public void Get_AddedKey_ReturnsValue() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); var result = cache.Get <String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); }
public ActionResult Index() { if (Logger == null) { Logger = new ListLogger(); } var stopwatch = new Stopwatch(); var result = new MyViewModel(); stopwatch.Start(); var cache = new RedisCacheProvider(RedisConnection); cache.Logger = Logger; stopwatch.Stop(); result.SetupTimeMs = stopwatch.ElapsedMilliseconds; stopwatch.Reset(); stopwatch.Start(); var item = cache.Get<Models.SampleModel>("models:sample1"); if (item == null) { item = new SampleModel() { DateOfBirth = DateTime.Now, Name = "Hello", Surname = "World", Other = new List<OtherModels>() { new OtherModels(){Foo = 1, Bar = 20.928}, new OtherModels(){Foo = 31, Bar = 30.3328}, } }; cache.Set("models:sample1", item, DateTime.Now.AddSeconds(5), new List<string>(){"Test1", "Test2"}); } stopwatch.Stop(); result.LoadTimeMs = stopwatch.ElapsedMilliseconds; result.Data = item; return View(result); }
public void GetByTag_SingleItemManyTags_ReturnsSingleValue() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); var tags = new List <string> { "tag1", "tag2", "tag3" }; cache.Set(key, value, expires, tags); var results = cache.GetByTag <String>("tag2"); Assert.IsNotNull(results); Assert.IsTrue(results.Count > 0); }
public void Ctor_Configuration__Fails() { var redis = new RedisConnectionManager("localhost"); var config = NewCacheConfiguration(redis); config.RedisClientConfiguration = new RedisClientConfiguration(redis) { Host = "nohost", TimeoutMilliseconds = 500 }; var cache = new RedisCacheProvider(config); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); Assert.Fail("Exception should be thrown with bad connections"); }
public void Get_ExpiredDate_RemovesTags() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); var config = NewCacheConfiguration(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; var tag = "remove tag"; DateTime expires = new DateTime(2000, 12, 11); cache.Set(key, value, expires, tag); var test = cache.Get <String>(key); var tagManager = new RedisTagManager(config.CacheItemFactory); var result = tagManager.GetKeysForTag(newRedisClient(), tag); Assert.AreEqual(result.Count(x => x == tag), 0); }
public void Ctor_Configuration__Fails() { var redis = new RedisConnectionManager("localhost"); var config = NewCacheConfiguration(redis); config.RedisClientConfiguration = new RedisClientConfiguration(redis) { Host = "nohost", TimeoutMilliseconds = 500 }; var cache = new RedisCacheProvider(config); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); Assert.Fail("Exception should be thrown with bad connections"); }
protected static void RedisCache_Absolute_Expire(RedisCacheProvider redisCache) { Console.WriteLine("Sync test absolute expire start..."); Console.Write(Environment.NewLine); var key = Guid.NewGuid().ToString("N"); var value = "something"; var options = CacheEntryOptionsFactory.AbSolute().Timeout(TimeSpan.FromMilliseconds(300)); redisCache.Set(key, value, options); Console.WriteLine(string.Format("Set key:{0} with value:{1} by expire:{2}", key, value, options.ExpirationTimeout.TotalMilliseconds)); Console.WriteLine(string.Format("Get key:{0} with value:{1}", key, redisCache.Get <string>(key))); Thread.Sleep(300); Console.WriteLine(string.Format("After expire:{0} get key:{1} with value:{2}", options.ExpirationTimeout.TotalMilliseconds, key, redisCache.Get <string>(key))); Console.Write(Environment.NewLine); Console.WriteLine("Sync test absolute expire end..."); }
static void Main(string[] args) { var key = "person"; using (var redis = new RedisCacheProvider(new DataConverter(), "localhost")) { var person = new Person { Name = "Alex", Age = 11 }; redis.Set(key, person); person = redis.Get <Person>(key); Console.WriteLine(person.Name); Console.WriteLine(person.Age); } Console.ReadKey(); }
public void ItemExpires_RemovedFromCache() { var redis = new RedisConnectionManager(); // this is just testing the built in expiry var config = new CacheConfiguration(redis); var cache = new RedisCacheProvider(config); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:ItemExpires_RemovedFromCache"; String value = "Hello World!"; DateTime expires = DateTime.Now.AddSeconds(3); string tag1 = "tag1"; string tag2 = "tag2"; cache.Set(key, value, expires, new List <string> { tag1, tag2 }); var result = cache.Get <String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); Thread.Sleep(1000); result = cache.Get <String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); Thread.Sleep(2500); result = cache.Get <String>(key); Assert.IsNull(result); }
public void Ctor_Configuration__Succeeds() { var redis = new RedisConnectionManager("localhost"); var config = NewCacheConfiguration(redis); config.RootNameSpace = "_TestRootNamespace"; config.Serializer = new BinarySerializationProvider(); config.RedisClientConfiguration = new RedisClientConfiguration(config.RedisClientConfiguration.RedisConnectionManagerConnectionManager) { Host = _redisHost, DbNo = _redisDB, TimeoutMilliseconds = 50 }; var cache = new RedisCacheProvider(config); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); // no exception }
public void Get_AddedObject_ReturnsValue() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; var value = new TestObject() { Foo = "Hello", Bar = "World", Score = 11 }; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); var result = cache.Get<TestObject>(key); Assert.IsNotNull(result); Assert.AreEqual(value.Foo, result.Foo); Assert.AreEqual(value.Bar, result.Bar); Assert.AreEqual(value.Score, result.Score); }
public IActionResult TestSetRedis() { _cacheProvider.Set <string>("Hello", "World"); return(Content("Redis item is added!")); }
public void ItemExpires_Tag_RemovedFromCache() { // this is testing that when expiry happens, the events kick in and remove the tags // see RedisExpireHandler var redis = new RedisConnectionManager(); var config = new CacheConfiguration(redis); var cache = new RedisCacheProvider(config); var client = newRedisClient(config.RedisClientConfiguration); cache.Logger = new TestRedisLogger(); Console.WriteLine("Start Logger"); string key = "TagCacheTests:ItemExpires_Tag_RemovedFromCache"; String value = "Hello World!"; DateTime expires = DateTime.Now.AddSeconds(3); string tag1 = "tag1001"; string tag2 = "tag1002"; cache.Set(key, value, expires, new List<string>{tag1, tag2}); // first check everything has been set var result = cache.Get<String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); var keysForTag1 = client.GetKeysForTag(tag1); Assert.IsNotNull(keysForTag1); Assert.IsTrue(keysForTag1.Any(x => x == key)); var tagsForKey = client.GetTagsForKey(key); Assert.IsNotNull(tagsForKey); Assert.IsTrue(tagsForKey.Any(x => x == tag1)); Assert.IsTrue(tagsForKey.Any(x => x == tag2)); // wait a while Thread.Sleep(1000); // check it has not expired yet result = cache.Get<String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); keysForTag1 = client.GetKeysForTag(tag1); Assert.IsNotNull(keysForTag1); Assert.IsTrue(keysForTag1.Any(x => x == key)); tagsForKey = client.GetTagsForKey(key); Assert.IsNotNull(tagsForKey); Assert.IsTrue(tagsForKey.Any(x => x == tag1)); Assert.IsTrue(tagsForKey.Any(x => x == tag2)); // now wait until it should have been removed Thread.Sleep(2500); // now check its all been removed result = cache.Get<String>(key); Assert.IsNull(result); keysForTag1 = client.GetKeysForTag(tag1); Assert.IsNotNull(keysForTag1); Assert.IsFalse(keysForTag1.Any(x => x == key)); tagsForKey = client.GetTagsForKey(key); Assert.IsNotNull(tagsForKey); Assert.IsFalse(tagsForKey.Any(x => x == tag1)); Assert.IsFalse(tagsForKey.Any(x => x == tag2)); }
public void Get_ExpiredDate_RemovesTags() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); var config = NewCacheConfiguration(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; var tag = "remove tag"; DateTime expires = new DateTime(2000, 12, 11); cache.Set(key, value, expires, tag); var test = cache.Get<String>(key); var tagManager = new RedisTagManager(config.CacheItemFactory); var result = tagManager.GetKeysForTag(newRedisClient(), tag); Assert.AreEqual(result.Count(x => x == tag), 0); }
public void ItemExpires_Tag_RemovedFromCache() { // this is testing that when expiry happens, the events kick in and remove the tags // see RedisExpireHandler var redis = new RedisConnectionManager(); var config = new CacheConfiguration(redis); var cache = new RedisCacheProvider(config); var client = newRedisClient(config.RedisClientConfiguration); cache.Logger = new TestRedisLogger(); Console.WriteLine("Start Logger"); string key = "TagCacheTests:ItemExpires_Tag_RemovedFromCache"; String value = "Hello World!"; DateTime expires = DateTime.Now.AddSeconds(3); string tag1 = "tag1001"; string tag2 = "tag1002"; cache.Set(key, value, expires, new List <string> { tag1, tag2 }); // first check everything has been set var result = cache.Get <String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); var keysForTag1 = client.GetKeysForTag(tag1); Assert.IsNotNull(keysForTag1); Assert.IsTrue(keysForTag1.Any(x => x == key)); var tagsForKey = client.GetTagsForKey(key); Assert.IsNotNull(tagsForKey); Assert.IsTrue(tagsForKey.Any(x => x == tag1)); Assert.IsTrue(tagsForKey.Any(x => x == tag2)); // wait a while Thread.Sleep(1000); // check it has not expired yet result = cache.Get <String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); keysForTag1 = client.GetKeysForTag(tag1); Assert.IsNotNull(keysForTag1); Assert.IsTrue(keysForTag1.Any(x => x == key)); tagsForKey = client.GetTagsForKey(key); Assert.IsNotNull(tagsForKey); Assert.IsTrue(tagsForKey.Any(x => x == tag1)); Assert.IsTrue(tagsForKey.Any(x => x == tag2)); // now wait until it should have been removed Thread.Sleep(2500); // now check its all been removed result = cache.Get <String>(key); Assert.IsNull(result); keysForTag1 = client.GetKeysForTag(tag1); Assert.IsNotNull(keysForTag1); Assert.IsFalse(keysForTag1.Any(x => x == key)); tagsForKey = client.GetTagsForKey(key); Assert.IsNotNull(tagsForKey); Assert.IsFalse(tagsForKey.Any(x => x == tag1)); Assert.IsFalse(tagsForKey.Any(x => x == tag2)); }
public void GetByTag_SingleItemManyTags_ReturnsSingleValue() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); var tags = new List<string> { "tag1", "tag2", "tag3" }; cache.Set(key, value, expires, tags); var results = cache.GetByTag<String>("tag2"); Assert.IsNotNull(results); Assert.IsTrue(results.Count > 0); }
public void RemoveByTag_ManyItemSingleTag_ReturnsNoValues() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key1 = "TagCacheTests:Add1"; string key2 = "TagCacheTests:Add2"; string key3 = "TagCacheTests:Add3"; String value1 = "Hello World!"; String value3 = "Two"; String value2 = "Three"; DateTime expires = new DateTime(2099, 12, 11); var tags = new List<string> { "another tag" }; cache.Set(key1, value1, expires, tags); cache.Set(key2, value2, expires, tags); cache.Set(key3, value3, expires, tags); var results = cache.GetByTag<String>(tags[0]); Assert.IsNotNull(results); Assert.AreEqual(results.Count, 3); cache.RemoveByTag(tags[0]); results = cache.GetByTag<String>(tags[0]); Assert.IsNull(results); }
public void Get_AddedKey_ReturnsValue() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2099, 12, 11); cache.Set(key, value, expires); var result = cache.Get<String>(key); Assert.IsNotNull(result); Assert.AreEqual(value, result); }
public void Get_ExpiredDate_RemovesFromCache() { var redis = new RedisConnectionManager(); var cache = new RedisCacheProvider(redis); cache.Logger = new TestRedisLogger(); string key = "TagCacheTests:Add"; String value = "Hello World!"; DateTime expires = new DateTime(2000, 12, 11); cache.Set(key, value, expires); var result = cache.Get<String>(key); Assert.IsNull(result); }