public DistributedCacheAccessor(IDistributedCache?distributedCache, IFusionCacheSerializer?serializer, FusionCacheOptions options, ILogger?logger) { Cache = distributedCache; Serializer = serializer; _options = options; _breakDuration = distributedCache is null ? TimeSpan.Zero : options.DistributedCacheCircuitBreakerDuration; _breakDurationTicks = _breakDuration.Ticks; _gatewayTicks = DateTimeOffset.MinValue.Ticks; _logger = logger; }
public MemoryCacheAccessor(IMemoryCache?memoryCache, FusionCacheOptions options, ILogger?logger) { if (memoryCache is object) { _cache = memoryCache; } else { _cache = new MemoryCache(new MemoryCacheOptions()); _cacheShouldBeDisposed = true; } _options = options; _logger = logger; }
public DistributedCacheAccessor(IDistributedCache distributedCache, IFusionCacheSerializer serializer, FusionCacheOptions options, ILogger?logger) { if (distributedCache == null) { throw new ArgumentNullException(nameof(distributedCache)); } if (serializer == null) { throw new ArgumentNullException(nameof(serializer)); } _cache = distributedCache; _serializer = serializer; _options = options; _breakDuration = distributedCache is null ? TimeSpan.Zero : options.DistributedCacheCircuitBreakerDuration; _breakDurationTicks = _breakDuration.Ticks; _gatewayTicks = DateTimeOffset.MinValue.Ticks; _logger = logger; }
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"); } }