Last active
February 10, 2023 09:22
-
-
Save suhrab/dab0a8081066cb51b130c744d93b4a86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ZiggyCreatures.Caching.Fusion; | |
namespace WorkerService8; | |
public class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
IHost host = Host.CreateDefaultBuilder(args) | |
.ConfigureServices(services => | |
{ | |
services.AddFusionCache(opts => | |
{ | |
opts.DefaultEntryOptions.IsFailSafeEnabled = true; | |
opts.DefaultEntryOptions.Duration = TimeSpan.FromHours(1); | |
opts.DefaultEntryOptions.FailSafeMaxDuration = TimeSpan.FromSeconds(10); | |
opts.DefaultEntryOptions.FailSafeThrottleDuration = TimeSpan.FromSeconds(5); | |
}); | |
}) | |
.Build(); | |
var cache = host.Services.GetRequiredService<IFusionCache>(); | |
await cache.GetOrSetAsync("key1", cacheFactoryGood, cacheSetup); | |
await Task.Delay(TimeSpan.FromSeconds(11)); | |
await cache.GetOrSetAsync("key1", cacheFactoryGood, cacheSetup); | |
await Task.Delay(TimeSpan.FromSeconds(11)); | |
await cache.GetOrSetAsync("key1", cacheFactoryGood, cacheSetup); | |
// local functions | |
static Task<CacheItem> cacheFactoryGood(CancellationToken cancellationToken) | |
{ | |
Console.WriteLine("cacheFactoryGood fired"); | |
return Task.FromResult(new CacheItem()); | |
} | |
static void cacheSetup(FusionCacheEntryOptions opts) | |
{ | |
opts.Duration = TimeSpan.FromHours(1); | |
opts.FailSafeMaxDuration = TimeSpan.FromSeconds(10); | |
opts.FailSafeThrottleDuration = TimeSpan.FromSeconds(5); | |
} | |
} | |
} | |
internal class CacheItem | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"cacheFactoryGood fired" will be printed to Console 3 times. Though only single time expected - Duration is 1hours