From 356bb074d5f9a60a778e46bc577ed238a987d385 Mon Sep 17 00:00:00 2001 From: zpc Date: Wed, 1 Apr 2026 19:47:20 +0800 Subject: [PATCH] 21 --- .../Liveforum/T_StreamersService.cs | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/server/admin/ZrAdminNetCore/ZR.Service/Liveforum/T_StreamersService.cs b/server/admin/ZrAdminNetCore/ZR.Service/Liveforum/T_StreamersService.cs index eb7de4d..a4214c1 100644 --- a/server/admin/ZrAdminNetCore/ZR.Service/Liveforum/T_StreamersService.cs +++ b/server/admin/ZrAdminNetCore/ZR.Service/Liveforum/T_StreamersService.cs @@ -162,18 +162,42 @@ namespace ZR.Service.Liveforum /// 清除的缓存key数量 public int ClearAllFlowerCache() { - if (RedisServer.WebApiCache == null) + var logger = NLog.LogManager.GetCurrentClassLogger(); + + // 按需获取WebAPI Redis连接,不依赖启动时初始化 + var webApiRedis = RedisServer.WebApiCache; + if (webApiRedis == null) { - throw new Exception("WebAPI Redis未配置,请检查appsettings.json中的RedisServer:WebApiCache配置"); + var config = Infrastructure.AppSettings.GetConfig("RedisServer:WebApiCache"); + logger.Info("RedisServer:WebApiCache 配置值: [{0}], IsNull: {1}, IsEmpty: {2}", + config ?? "(null)", config == null, string.IsNullOrEmpty(config)); + + // 打印其他Redis配置做对比 + var cacheConfig = Infrastructure.AppSettings.GetConfig("RedisServer:Cache"); + var openConfig = Infrastructure.AppSettings.GetConfig("RedisServer:open"); + logger.Info("RedisServer:Cache 配置值: [{0}]", cacheConfig ?? "(null)"); + logger.Info("RedisServer:open 配置值: [{0}]", openConfig ?? "(null)"); + + if (string.IsNullOrEmpty(config)) + { + throw new Exception($"WebAPI Redis未配置。RedisServer:open=[{openConfig}], RedisServer:Cache=[{(string.IsNullOrEmpty(cacheConfig) ? "(空)" : "已配置")}], RedisServer:WebApiCache=[(空)]。请在appsettings.json的RedisServer节点下添加WebApiCache配置项"); + } + + logger.Info("正在创建WebAPI Redis连接: {0}", config); + webApiRedis = new CSRedis.CSRedisClient(config); + RedisServer.WebApiCache = webApiRedis; } - var keys = RedisServer.WebApiCache.Keys("flower_count:Streamer:*"); + var keys = webApiRedis.Keys("flower_count:Streamer:*"); + logger.Info("找到 {0} 个flower_count:Streamer:* 缓存key", keys?.Length ?? 0); + if (keys == null || keys.Length == 0) { return 0; } - RedisServer.WebApiCache.Del(keys); + webApiRedis.Del(keys); + logger.Info("已清除 {0} 个送花缓存key", keys.Length); return keys.Length; }