Skip to content

Instantly share code, notes, and snippets.

@suhrab
Last active February 10, 2023 09:22
Show Gist options
  • Save suhrab/dab0a8081066cb51b130c744d93b4a86 to your computer and use it in GitHub Desktop.
Save suhrab/dab0a8081066cb51b130c744d93b4a86 to your computer and use it in GitHub Desktop.
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
{
}
@suhrab
Copy link
Author

suhrab commented Feb 10, 2023

"cacheFactoryGood fired" will be printed to Console 3 times. Though only single time expected - Duration is 1hours

@jodydonetti
Copy link

Hi, please see my asnwer ZiggyCreatures/FusionCache#115 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment