-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
97 lines (78 loc) · 3.37 KB
/
Copy pathProgram.cs
File metadata and controls
97 lines (78 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Microsoft.Extensions.Caching.StackExchangeRedis;
using Serilog;
using WeatherApi.Options;
using WeatherApi.Services;
using ZiggyCreatures.Caching.Fusion;
using ZiggyCreatures.Caching.Fusion.Backplane.StackExchangeRedis;
using ZiggyCreatures.Caching.Fusion.Serialization.SystemTextJson;
var builder = WebApplication.CreateBuilder(args);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Information)
.WriteTo.Console()
.CreateLogger();
builder.Services.AddLogging(configure => configure.AddSerilog());
builder.Services.AddControllers();
builder.Services.AddOpenApi();
builder.Services.AddHttpClient();
builder.Services.Configure<WeatherOptions>(builder.Configuration.GetSection("WeatherOptions"));
builder.Services.AddKeyedSingleton<IWeatherService, WeatherServiceConcurrentDictionary>("concurrentDict");
builder.Services.AddKeyedSingleton<IWeatherService, WeatherServiceConcurrentDictionarySemaphore>(
"concurrentDict_semaphore");
builder.Services.AddKeyedSingleton<IWeatherService, WeatherServiceConcurrentDictionaryLazy>("concurrentDict_lazy");
builder.Services.AddKeyedSingleton<IWeatherService, WeatherServiceHybridCache>("hybridCache");
builder.Services.AddMemoryCache();
//AddHybridCache(builder.Services);
AddHybridFusionCache(builder.Services);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
return;
void AddHybridFusionCache(IServiceCollection serviceCollection)
{
serviceCollection.AddFusionCache()
.WithOptions(options =>
{
// DISTRIBUTED CACHE CIRCUIT-BREAKER
options.DistributedCacheCircuitBreakerDuration = TimeSpan.FromSeconds(2);
})
.WithDefaultEntryOptions(options =>
{
options.Duration = TimeSpan.FromMinutes(1);
// FAIL-SAFE OPTIONS
options.IsFailSafeEnabled = true;
options.FailSafeMaxDuration = TimeSpan.FromHours(2);
options.FailSafeThrottleDuration = TimeSpan.FromSeconds(30);
// FACTORY TIMEOUTS
options.FactorySoftTimeout = TimeSpan.FromMilliseconds(100);
options.FactoryHardTimeout = TimeSpan.FromMilliseconds(1500);
// DISTRIBUTED CACHE OPTIONS
options.DistributedCacheSoftTimeout = TimeSpan.FromSeconds(1);
options.DistributedCacheHardTimeout = TimeSpan.FromSeconds(2);
options.AllowBackgroundDistributedCacheOperations = true;
// JITTERING
options.JitterMaxDuration = TimeSpan.FromSeconds(2);
})
.WithSerializer(new FusionCacheSystemTextJsonSerializer())
.WithRegisteredMemoryCache()
.WithDistributedCache(new RedisCache(new RedisCacheOptions { Configuration = "localhost:6379" }))
.WithBackplane(
new RedisBackplane(new RedisBackplaneOptions { Configuration = "localhost:6379" })
)
.AsHybridCache();
}
void AddHybridCache(IServiceCollection serviceCollection)
{
#pragma warning disable EXTEXP0018
serviceCollection.AddHybridCache();
#pragma warning restore EXTEXP0018
serviceCollection.AddMemoryCache().AddStackExchangeRedisCache(options =>
{
options.Configuration = "localhost:6379";
});
}