public void DistributedCacheCircuitBreakerActuallyWorks() { var circuitBreakerDuration = TimeSpan.FromSeconds(2); var distributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); var chaosDistributedCache = new ChaosDistributedCache(distributedCache); using (var memoryCache = new MemoryCache(new MemoryCacheOptions())) { using (var fusionCache = new FusionCache(new FusionCacheOptions() { DistributedCacheCircuitBreakerDuration = circuitBreakerDuration }, memoryCache)) { fusionCache.DefaultEntryOptions.AllowBackgroundDistributedCacheOperations = false; fusionCache.SetupDistributedCache(chaosDistributedCache, new FusionCacheNewtonsoftJsonSerializer()); fusionCache.Set <int>("foo", 1, options => options.SetDurationSec(60).SetFailSafe(true)); chaosDistributedCache.SetAlwaysThrow(); fusionCache.Set <int>("foo", 2, options => options.SetDurationSec(60).SetFailSafe(true)); chaosDistributedCache.SetNeverThrow(); fusionCache.Set <int>("foo", 3, options => options.SetDurationSec(60).SetFailSafe(true)); Thread.Sleep(circuitBreakerDuration); memoryCache.Remove("foo"); var res = fusionCache.GetOrDefault <int>("foo", -1); Assert.Equal(1, res); } } }
public void FusionCache_MemoryProvider() { var fusionCache = new FusionCache(new FusionCacheOptions()); LoopAction(Iterations, () => { fusionCache.Set("TestKey", 123, TimeSpan.FromDays(1)); fusionCache.GetOrDefault <int>("TestKey"); fusionCache.GetOrSet("TestKey", (cancellationToken) => "Hello World", TimeSpan.FromDays(1)); }); }
public void HandlesFlexibleComplexTypeConversions(SerializerType serializerType) { using (var memoryCache = new MemoryCache(new MemoryCacheOptions())) { var distributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); using (var fusionCache = new FusionCache(new FusionCacheOptions(), memoryCache).SetupDistributedCache(distributedCache, GetSerializer(serializerType))) { var initialValue = (object)SampleComplexObject.CreateRandom(); fusionCache.Set("foo", initialValue, TimeSpan.FromHours(24)); memoryCache.Remove("foo"); var newValue = fusionCache.GetOrDefault <SampleComplexObject>("foo"); Assert.NotNull(newValue); } } }
async static Task Main(string[] args) { var cacheDurationSec = 5; var failSafeMaxDurationSec = 30; var failSafeThrottleDurationSec = 3; var factoryTimeoutMs = 2_000; var useFailSafe = true; var useDistributedCache = true; Console.OutputEncoding = Encoding.UTF8; // DI var services = new ServiceCollection(); SetupSerilogLogger(services); //SetupStandardLogger(services); var serviceProvider = services.BuildServiceProvider(); var logger = serviceProvider.GetService <ILogger <FusionCache> >(); // CACHE OPTIONS var options = new FusionCacheOptions { CacheKeyPrefix = "dev:", DefaultEntryOptions = new FusionCacheEntryOptions { Duration = TimeSpan.FromSeconds(cacheDurationSec), FailSafeMaxDuration = TimeSpan.FromSeconds(failSafeMaxDurationSec), FailSafeThrottleDuration = TimeSpan.FromSeconds(failSafeThrottleDurationSec), Priority = CacheItemPriority.NeverRemove, IsFailSafeEnabled = useFailSafe, FactorySoftTimeout = TimeSpan.FromMilliseconds(factoryTimeoutMs), AllowBackgroundDistributedCacheOperations = true }, }; using (var fusionCache = new FusionCache(options, logger: logger)) { if (useDistributedCache) { // DISTRIBUTED CACHE var serializer = new FusionCacheNewtonsoftJsonSerializer(); var distributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); Console.WriteLine(); fusionCache.SetupDistributedCache(distributedCache, serializer); Console.WriteLine(); } await fusionCache.SetAsync <int>("foo", 42, options => options.SetDurationSec(1).SetFailSafe(true, TimeSpan.FromMinutes(1))); Console.WriteLine(); Console.WriteLine($"initial: {fusionCache.GetOrDefault<int>("foo")}"); await Task.Delay(1_500); Console.WriteLine(); var tmp1 = await fusionCache.GetOrSetAsync <int>("foo", async _ => { await Task.Delay(2_000); return(21); }, options => options.SetDurationSec(1).SetFailSafe(true).SetFactoryTimeouts(1_000)); Console.WriteLine(); Console.WriteLine($"tmp1: {tmp1}"); await Task.Delay(2_500); Console.WriteLine(); var tmp2 = await fusionCache.GetOrDefaultAsync <int>("foo", options => options.SetDurationSec(1).SetFailSafe(true).SetFactoryTimeouts(1_000)); Console.WriteLine(); Console.WriteLine($"tmp2: {tmp2}"); await Task.Delay(2_500); Console.WriteLine(); var tmp3 = await fusionCache.GetOrSetAsync <int>("foo", async _ => { await Task.Delay(2_000); throw new Exception("Sloths are cool"); }, options => options.SetDurationSec(1).SetFailSafe(true).SetFactoryTimeouts(1_000)); Console.WriteLine(); Console.WriteLine($"tmp3: {tmp3}"); await Task.Delay(2_500); Console.WriteLine(); var tmp4 = await fusionCache.GetOrSetAsync <int>("foo", async _ => { await Task.Delay(2_000); return(666); }, options => options.SetDurationSec(1).SetFailSafe(true).SetFactoryTimeouts(1_000, keepTimedOutFactoryResult: false)); Console.WriteLine(); Console.WriteLine($"tmp4: {tmp4}"); await Task.Delay(2_500); Console.WriteLine(); var tmp5 = await fusionCache.GetOrDefaultAsync <int>("foo", options => options.SetDurationSec(1).SetFailSafe(true).SetFactoryTimeouts(1_000)); Console.WriteLine(); Console.WriteLine($"tmp5: {tmp5}"); Console.WriteLine(); await fusionCache.SetAsync("foo", 123, fusionCache.CreateEntryOptions(entry => entry.SetDurationSec(1).SetFailSafe(true))); await Task.Delay(1_500); Console.WriteLine(); await fusionCache.GetOrSetAsync <int>("foo", _ => { throw new Exception("Foo"); }, options => options.SetDurationSec(1).SetFailSafe(true).SetFactoryTimeouts(1_000)); Console.WriteLine(); await fusionCache.SetAsync("foo", 123, options => options.SetDurationSec(1).SetFailSafe(true)); await Task.Delay(1_500); Console.WriteLine(); await fusionCache.GetOrSetAsync <int>("foo", async _ => { await Task.Delay(2_000); throw new Exception("Foo"); }, options => options.SetDurationSec(1).SetFailSafe(true).SetFactoryTimeouts(1_000)); Console.WriteLine(); await Task.Delay(2_500); Console.WriteLine("THE END"); } }