|
15 | 15 | using OpenBioCardServer.Constants; |
16 | 16 | using OpenBioCardServer.Interfaces; |
17 | 17 | using OpenBioCardServer.Structs.ENums; |
| 18 | +using ZiggyCreatures.Caching.Fusion; |
| 19 | +using ZiggyCreatures.Caching.Fusion.Serialization.SystemTextJson; |
18 | 20 |
|
19 | 21 | namespace OpenBioCardServer; |
20 | 22 |
|
@@ -246,21 +248,70 @@ private static void ConfigureCacheService(WebApplicationBuilder builder) |
246 | 248 | { |
247 | 249 | var cacheSettings = builder.Configuration.GetSection(CacheSettings.SectionName).Get<CacheSettings>() ?? new CacheSettings(); |
248 | 250 |
|
| 251 | + if (!cacheSettings.Enabled) |
| 252 | + { |
| 253 | + Console.WriteLine("==> Cache is DISABLED via configuration."); |
| 254 | + |
| 255 | + builder.Services.AddFusionCache() |
| 256 | + .WithDefaultEntryOptions(new FusionCacheEntryOptions |
| 257 | + { |
| 258 | + Duration = TimeSpan.Zero, |
| 259 | + IsFailSafeEnabled = false, |
| 260 | + FactorySoftTimeout = Timeout.InfiniteTimeSpan, |
| 261 | + AllowBackgroundDistributedCacheOperations = false |
| 262 | + }); |
| 263 | + |
| 264 | + return; |
| 265 | + } |
| 266 | + |
249 | 267 | builder.Services.AddMemoryCache(options => |
250 | 268 | { |
251 | 269 | options.SizeLimit = cacheSettings.CacheSizeLimit ?? 100; |
252 | 270 | options.CompactionPercentage = cacheSettings.CompactionPercentage; |
253 | 271 | }); |
254 | 272 |
|
255 | | - if (cacheSettings.UseRedis && !string.IsNullOrEmpty(cacheSettings.RedisConnectionString)) |
| 273 | + var fusionBuilder = builder.Services.AddFusionCache() |
| 274 | + .WithOptions(options => |
| 275 | + { |
| 276 | + options.DistributedCacheCircuitBreakerDuration = TimeSpan.FromSeconds(cacheSettings.DistributedCacheCircuitBreakerDurationSeconds); |
| 277 | + }) |
| 278 | + .WithDefaultEntryOptions(new FusionCacheEntryOptions |
| 279 | + { |
| 280 | + Duration = TimeSpan.FromMinutes(cacheSettings.ExpirationMinutes), |
| 281 | + |
| 282 | + FactorySoftTimeout = TimeSpan.FromMilliseconds(cacheSettings.FactorySoftTimeoutMilliseconds), |
| 283 | + |
| 284 | + IsFailSafeEnabled = cacheSettings.EnableFailSafe, |
| 285 | + FailSafeMaxDuration = TimeSpan.FromMinutes(cacheSettings.FailSafeMaxDurationMinutes), |
| 286 | + FailSafeThrottleDuration = TimeSpan.FromSeconds(30), |
| 287 | + }) |
| 288 | + .WithSerializer(new FusionCacheSystemTextJsonSerializer()); |
| 289 | + |
| 290 | + // 根据配置决定是否启用 Redis 和 Backplane |
| 291 | + if (cacheSettings.Enabled && cacheSettings.UseRedis && !string.IsNullOrEmpty(cacheSettings.RedisConnectionString)) |
256 | 292 | { |
257 | 293 | builder.Services.AddStackExchangeRedisCache(options => |
258 | 294 | { |
259 | 295 | options.Configuration = cacheSettings.RedisConnectionString; |
260 | 296 | options.InstanceName = cacheSettings.InstanceName; |
261 | 297 | }); |
| 298 | + |
| 299 | + // 将 Redis 注册为 FusionCache 的二级缓存 |
| 300 | + fusionBuilder.WithRegisteredDistributedCache(); |
| 301 | + |
| 302 | + // 注册 Redis Backplane (用于集群缓存同步) |
| 303 | + fusionBuilder.WithStackExchangeRedisBackplane(options => |
| 304 | + { |
| 305 | + options.Configuration = cacheSettings.RedisConnectionString; |
| 306 | + }); |
| 307 | + |
| 308 | + Console.WriteLine("==> FusionCache configured with Redis L2 & Backplane."); |
| 309 | + } |
| 310 | + else |
| 311 | + { |
| 312 | + Console.WriteLine("==> FusionCache configured in Memory-Only mode."); |
262 | 313 | } |
263 | | - builder.Services.AddSingleton<ICacheService, CacheService>(); |
| 314 | + // builder.Services.AddSingleton<ICacheService, CacheService>(); |
264 | 315 | } |
265 | 316 |
|
266 | 317 | private static void ConfigureCompressionService(WebApplicationBuilder builder) |
|
0 commit comments