public void Redis_UseExistingConnection() { var conConfig = new ConfigurationOptions() { ConnectTimeout = 10000, AbortOnConnectFail = false, ConnectRetry = 10 }; conConfig.EndPoints.Add("localhost:6379"); var multiplexer = ConnectionMultiplexer.Connect(conConfig); var cfg = ConfigurationBuilder.BuildConfiguration( s => s .WithJsonSerializer() .WithRedisConfiguration("redisKey", multiplexer) .WithRedisCacheHandle("redisKey")); RedisConnectionManager.RemoveConnection(multiplexer.Configuration); using (multiplexer) using (var cache = new BaseCacheManager <long>(cfg)) { cache.Add(Guid.NewGuid().ToString(), 12345); } }
private static void MultiCacheEvictionWithoutRedisCacheHandle() { var config = new ConfigurationBuilder("Redis with Redis Backplane") .WithDictionaryHandle(true) .WithExpiration(ExpirationMode.Absolute, TimeSpan.FromSeconds(5)) .And .WithRedisBackplane("redisConfig") .WithRedisConfiguration("redisConfig", "localhost,allowadmin=true", enableKeyspaceNotifications: true) //.WithMicrosoftLogging(new LoggerFactory().AddConsole(LogLevel.Debug)) .Build(); var cacheA = new BaseCacheManager <string>(config); var cacheB = new BaseCacheManager <string>(config); var key = "someKey"; cacheA.OnRemove += (s, args) => { Console.WriteLine("A triggered remove: " + args.ToString() + " - key still exists? " + cacheA.Exists(key)); }; cacheB.OnRemove += (s, args) => { Console.WriteLine("B triggered remove: " + args.ToString() + " - key still exists? " + cacheB.Exists(key)); }; cacheA.OnRemoveByHandle += (s, args) => { cacheA.Remove(args.Key); Console.WriteLine("A triggered removeByHandle: " + args.ToString() + " - key still exists? " + cacheA.Exists(key)); }; cacheB.OnRemoveByHandle += (s, args) => { Console.WriteLine("B triggered removeByHandle: " + args.ToString() + " - key still exists? " + cacheA.Exists(key) + " in A? " + cacheA.Exists(key)); }; cacheA.OnAdd += (s, args) => { Console.WriteLine("A triggered add: " + args.ToString()); }; cacheB.OnAdd += (s, args) => { Console.WriteLine("B triggered add: " + args.ToString()); }; Console.WriteLine("Add to A: " + cacheA.Add(key, "some value")); Console.WriteLine("Add to B: " + cacheB.Add(key, "some value")); Thread.Sleep(2000); cacheA.Remove(key); }
public void Redis_UseExistingConnection() { var multiplexer = ConnectionMultiplexer.Connect("localhost:6379"); var cfg = ConfigurationBuilder.BuildConfiguration( s => s .WithJsonSerializer() .WithRedisConfiguration("redisKey", multiplexer) .WithRedisCacheHandle("redisKey")); using (var cache = new BaseCacheManager <long>(cfg)) { cache.Add("somevalue", 12345); } }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { var result = _cache.Get(KeyPrefix); if (result != null) { return(input.CreateMethodReturn(result)); } IMethodReturn methodReturn = getNext()(input, getNext); _cache.Add(KeyPrefix, methodReturn.ReturnValue); return(methodReturn); }
public async Task Events_OnRemoveExternal_Redis_NoneHandling() { var client = ConnectionMultiplexer.Connect("localhost"); var config = new ConfigurationBuilder() .WithUpdateMode(CacheUpdateMode.None) .WithDictionaryHandle() .And .WithJsonSerializer() .WithRedisConfiguration("redis", client, enableKeyspaceNotifications: true) .WithRedisCacheHandle("redis") .Build(); var key = Guid.NewGuid().ToString(); var onRemoveByHandleValid = false; var onRemoveValid = false; var cache = new BaseCacheManager <int?>(config); cache.OnRemoveByHandle += (s, args) => { if (args.Reason == CacheItemRemovedReason.ExternalDelete && args.Key == key) { onRemoveByHandleValid = true; } }; cache.OnRemove += (s, args) => { if (args.Key == key) { onRemoveValid = true; } }; cache.Add(key, 1234).Should().BeTrue(); var x = cache.Get(key); client.GetDatabase(0).KeyDelete(key); await Task.Delay(1000); onRemoveByHandleValid.Should().BeTrue("onRemoveByHandle Event should have been raised"); onRemoveValid.Should().BeTrue("onRemove Event should have been raised"); cache.CacheHandles.First().Get(key).Should().Be(1234); }
public async Task <CacheItemRemovedEventArgs> RunTest(ICacheManagerConfiguration configuration, string useKey, string useRegion, bool endGetShouldBeNull = true, bool runGetWhileWaiting = true) { var triggered = false; CacheItemRemovedEventArgs resultArgs = null; var cache = new BaseCacheManager <string>(configuration); cache.OnRemoveByHandle += (sender, args) => { triggered = true; resultArgs = args; }; if (useRegion == null) { cache.Add(useKey, "value"); cache.Get(useKey).Should().NotBeNull(); } else { cache.Add(useKey, "value", useRegion); cache.Get(useKey, useRegion).Should().NotBeNull(); } // sys runtime checks roughly every 10 seconds, there is no other way to test this quicker I think var count = 0; while (count < 30 && !triggered) { if (runGetWhileWaiting) { if (useRegion == null) { cache.CacheHandles.ToList().ForEach(p => p.Get(useKey)); } else { cache.CacheHandles.ToList().ForEach(p => p.Get(useKey, useRegion)); } } await Task.Delay(1000); count++; } if (!triggered) { throw new Exception("Waited pretty long, no events triggered..."); } // validate on Up update mode, the handles above have been cleaned up for example if (endGetShouldBeNull) { if (useRegion == null) { cache.Get(useKey).Should().BeNull(); } else { cache.Get(useKey, useRegion).Should().BeNull(); } } return(resultArgs); }
public static void Main(string[] args) { var configBuilder = new Microsoft.Extensions.Configuration.ConfigurationBuilder() .AddJsonFile("cache.json"); var configuration = configBuilder.Build().GetCacheConfiguration(); var cache = new BaseCacheManager <string>(configuration); cache.Add("key", "value"); int iterations = int.MaxValue; try { var builder = new Core.ConfigurationBuilder("myCache"); builder.WithMicrosoftLogging(f => { f.AddConsole(LogLevel.Warning); f.AddDebug(LogLevel.Debug); }); builder.WithUpdateMode(CacheUpdateMode.Up); builder.WithRetryTimeout(1000); builder.WithMaxRetries(10); builder.WithDictionaryHandle() .DisableStatistics(); builder.WithRedisCacheHandle("redis", true) .DisableStatistics(); builder.WithRedisBackplane("redis"); builder.WithRedisConfiguration("redis", config => { config .WithAllowAdmin() .WithDatabase(0) .WithConnectionTimeout(5000) .WithEndpoint("127.0.0.1", 6379); }); builder.WithJsonSerializer(); Console.WriteLine("Using Redis cache handle"); var cacheA = new BaseCacheManager <object>(builder.Build()); cacheA.Clear(); for (int i = 0; i < iterations; i++) { try { Tests.PutAndMultiGetTest(cacheA); } catch (AggregateException ex) { ex.Handle((e) => { Console.WriteLine(e); return(true); }); } catch (Exception e) { Console.WriteLine("Error: " + e.Message + "\n" + e.StackTrace); Thread.Sleep(1000); } Console.WriteLine("---------------------------------------------------------"); } } catch (Exception ex) { Console.WriteLine(ex); } Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("We are done..."); Console.ForegroundColor = ConsoleColor.Gray; Console.ReadKey(); }