From e7dcdb2c58393888811c9f558db9745ea56ceac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Sat, 1 Jan 2022 16:05:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A0redis=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/CommonController.cs | 2 +- ZR.Admin.WebApi/Startup.cs | 8 ++++++ ZR.Admin.WebApi/appsettings.json | 4 +++ ZR.Common/Cache/CacheHelper.cs | 27 ++++++------------- ZR.Common/Cache/RedisServer.cs | 17 ++++++++++++ ZR.Common/ZR.Common.csproj | 1 + 6 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 ZR.Common/Cache/RedisServer.cs diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs index b3b7b52..83d461a 100644 --- a/ZR.Admin.WebApi/Controllers/CommonController.cs +++ b/ZR.Admin.WebApi/Controllers/CommonController.cs @@ -84,7 +84,7 @@ namespace ZR.Admin.WebApi.Controllers mailHelper.SendMail(toUsers, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent); logger.Info($"鍙戦侀偖浠秢JsonConvert.SerializeObject(sendEmailVo)}"); - + return SUCCESS(true); } diff --git a/ZR.Admin.WebApi/Startup.cs b/ZR.Admin.WebApi/Startup.cs index a32423e..b375704 100644 --- a/ZR.Admin.WebApi/Startup.cs +++ b/ZR.Admin.WebApi/Startup.cs @@ -11,10 +11,12 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.IO; +using System.Threading.Tasks; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; using ZR.Admin.WebApi.Framework; using ZR.Admin.WebApi.Middleware; +using ZR.Common.Cache; namespace ZR.Admin.WebApi { @@ -137,6 +139,12 @@ namespace ZR.Admin.WebApi services.AddTaskSchedulers(); //初始化db DbExtension.AddDb(configuration); + + //注册REDIS 服务 + Task.Run(() => + { + RedisServer.Initalize(); + }); } } } diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index e062474..2d66141 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -53,5 +53,9 @@ //协议 "Smtp": "smtp.qq.com", "Port": 587 + }, + "RedisServer": { + "Cache": "127.0.0.1:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=cache:", + "Session": "127.0.0.1:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=session:" } } diff --git a/ZR.Common/Cache/CacheHelper.cs b/ZR.Common/Cache/CacheHelper.cs index 9a4c8dc..0fc6625 100644 --- a/ZR.Common/Cache/CacheHelper.cs +++ b/ZR.Common/Cache/CacheHelper.cs @@ -1,11 +1,6 @@ -锘 -using Microsoft.Extensions.Caching.Memory; +锘縰sing Microsoft.Extensions.Caching.Memory; using System; -using System.Web; -/// -/// 鏁版嵁缂撳瓨鎿嶄綔绫 author by zhaorui 2021-3-6 -/// namespace ZR.Common { public class CacheHelper @@ -19,11 +14,6 @@ namespace ZR.Common }); } - /// - /// 鍏ㄥ眬缂撳瓨鏃堕棿 - /// - public static int CacheTime = 5; //ConfigHelper.GetConfigInt("CacheTime"); - /// /// 鑾峰彇缂撳瓨 /// @@ -46,7 +36,6 @@ namespace ZR.Common public static object GetCache(string CacheKey) { return Cache.Get(CacheKey); - //return HttpRuntime.Cache[CacheKey]; } public static object Get(string CacheKey) @@ -59,9 +48,9 @@ namespace ZR.Common /// /// key /// 鍊 - public static void SetCache(string CacheKey, object objObject) + public static object SetCache(string CacheKey, object objObject) { - Cache.Set(CacheKey, objObject); + return Cache.Set(CacheKey, objObject); } /// @@ -70,9 +59,9 @@ namespace ZR.Common /// key /// 鍊 /// 杩囨湡鏃堕棿锛堝垎閽燂級 - public static void SetCache(string CacheKey, object objObject, int Timeout) + public static object SetCache(string CacheKey, object objObject, int Timeout) { - Cache.Set(CacheKey, objObject, DateTime.Now.AddMinutes(Timeout)); + return Cache.Set(CacheKey, objObject, DateTime.Now.AddMinutes(Timeout)); } /// @@ -93,9 +82,9 @@ namespace ZR.Common /// 鍊 /// 杩囨湡鏃堕棿 /// 杩囨湡鏃堕棿闂撮殧 - public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) + public static object SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) { - Cache.Set(CacheKey, objObject, absoluteExpiration); + return Cache.Set(CacheKey, objObject, absoluteExpiration); } /// @@ -103,7 +92,7 @@ namespace ZR.Common /// /// /// - /// 瓒呰繃澶氬皯绉掑悗杩囨湡 + /// 瓒呰繃澶氬皯绉掑悗杩囨湡 public static void SetCacheDateTime(string CacheKey, object objObject, long Seconds) { Cache.Set(CacheKey, objObject, DateTime.Now.AddSeconds(Seconds)); diff --git a/ZR.Common/Cache/RedisServer.cs b/ZR.Common/Cache/RedisServer.cs new file mode 100644 index 0000000..3e99211 --- /dev/null +++ b/ZR.Common/Cache/RedisServer.cs @@ -0,0 +1,17 @@ +锘縰sing CSRedis; +using Infrastructure; + +namespace ZR.Common.Cache +{ + public class RedisServer + { + public static CSRedisClient Cache; + public static CSRedisClient Session; + + public static void Initalize() + { + Cache = new CSRedisClient(ConfigUtils.Instance.GetConfig("RedisServer:Cache")); + Session = new CSRedisClient(ConfigUtils.Instance.GetConfig("RedisServer:Session")); + } + } +} diff --git a/ZR.Common/ZR.Common.csproj b/ZR.Common/ZR.Common.csproj index 6c9ad0f..18265e5 100644 --- a/ZR.Common/ZR.Common.csproj +++ b/ZR.Common/ZR.Common.csproj @@ -6,6 +6,7 @@ +