林豪 左 hace 2 años
padre
commit
1a0cc02cb3

+ 1 - 1
DBHelper/DB.cs

@@ -1,6 +1,6 @@
 using SqlSugar;
 
-namespace WCS.Core.DbHelper
+namespace DBHelper
 {
     /// <summary>
     /// DB,禁止跨上下文使用

+ 2 - 0
DBHelper/DBHelper.csproj

@@ -7,6 +7,8 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="FreeRedis" Version="1.0.10" />
+    <PackageReference Include="LogHelper" Version="1.0.0.1" />
     <PackageReference Include="SqlSugarCore" Version="5.1.4.69" />
   </ItemGroup>
 

+ 1 - 1
DBHelper/DbContext.cs

@@ -1,6 +1,6 @@
 using SqlSugar;
 
-namespace WCS.Core.DbHelper
+namespace DBHelper
 {
     public class DbContext
     {

+ 3 - 4
DBHelper/DbLog.cs

@@ -1,7 +1,6 @@
-using System.Collections.Generic;
-using WCS.Core.Log;
+using LogHelper;
 
-namespace WCS.Core.DbHelper
+namespace DBHelper
 {
     public class DbLog : ILogType
     {
@@ -28,7 +27,7 @@ namespace WCS.Core.DbHelper
             {
                 ["DB"] = "Db"
             };
-            LogHelper.Init(this);
+            LogHelper.LogHelper.Init(this);
         }
 
         /// <summary>

+ 38 - 0
DBHelper/Redis/RedisClientList.cs

@@ -0,0 +1,38 @@
+using FreeRedis;
+
+namespace DBHelper.Redis
+{
+    /// <summary>
+    /// 链接集合
+    /// </summary>
+    public class RedisClientList
+    {
+        /// <summary>
+        /// 连接集合
+        /// </summary>
+        /// <param name="key">连接标识</param>
+        /// <param name="client">连接</param>
+        /// <param name="connectionConfig">连接字符串</param>
+        public RedisClientList(string key, RedisClient client, string connectionConfig)
+        {
+            Key = key;
+            Client = client;
+            ConnectionConfig = connectionConfig;
+        }
+
+        /// <summary>
+        /// 连接标识
+        /// </summary>
+        public string Key { get; set; }
+
+        /// <summary>
+        /// 连接
+        /// </summary>
+        public RedisClient Client { get; set; }
+
+        /// <summary>
+        /// 连接字符串
+        /// </summary>
+        public string ConnectionConfig { get; set; }
+    }
+}

+ 153 - 0
DBHelper/Redis/RedisHub.cs

@@ -0,0 +1,153 @@
+using FreeRedis;
+
+namespace DBHelper.Redis
+{
+    /// <summary>
+    /// Redis
+    /// </summary>
+    public static class RedisHub
+    {
+        /// <summary>
+        /// 连接集合
+        /// </summary>
+        private static List<RedisClientList> RedisClients = new List<RedisClientList>();
+
+        /// <summary>
+        /// 默认上下文类类型
+        /// </summary>
+        public static string DefaultContextType { get; private set; } = null!;
+
+        /// <summary>
+        /// 默认监控上下文类类型
+        /// </summary>
+        public static string MonitorContextType { get; private set; } = null!;
+
+        /// <summary>
+        /// 默认调试上下文类类型
+        /// </summary>
+        public static string DebugContextType { get; private set; } = null!;
+
+        /// <summary>
+        /// 设置默认链接
+        /// </summary>
+        /// <param name="key"></param>
+        public static void SetDefaultContextType(string key)
+        {
+            DefaultContextType = key;
+        }
+
+        /// <summary>
+        /// 设置监控链接
+        /// </summary>
+        /// <param name="key"></param>
+        public static void SetMonitorContextType(string key)
+        {
+            MonitorContextType = key;
+        }
+
+        /// <summary>
+        /// 设置监控链接
+        /// </summary>
+        /// <param name="key"></param>
+        public static void SetDebugContextType(string key)
+        {
+            DebugContextType = key;
+        }
+
+        /// <summary>
+        /// 默认链接
+        /// </summary>
+        public static RedisClient Default
+        {
+            get
+            {
+                {
+                    if (DefaultContextType == null)
+                        throw new Exception("请先设置默认RedisDB库,调用静态方法SetDefaultDbContextType()");
+                    return Context(DefaultContextType);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 监控连接
+        /// </summary>
+        public static RedisClient Monitor
+        {
+            get
+            {
+                {
+                    if (MonitorContextType == null)
+                        throw new Exception("请先设置监控RedisDB库,调用静态方法SetMonitorContextType()");
+                    return Context(MonitorContextType);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 监控连接
+        /// </summary>
+        public static RedisClient Debug
+        {
+            get
+            {
+                {
+                    if (MonitorContextType == null)
+                        throw new Exception("请先设置DebugRedisDB库,调用静态方法SetDebugContextType()");
+                    return Context(MonitorContextType);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 创建一个连接
+        /// </summary>
+        /// <param name="connectionString"> 连接字符串</param>
+        /// <param name="key">标识</param>
+        /// <param name="defaultClient"> 是否为默认连接,默认为false</param>
+        /// <returns></returns>
+        public static RedisClient CreateContext(string connectionString, string key, bool defaultClient = false)
+        {
+            var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
+            if (ctx != null) return ctx.Client;
+            ctx = new RedisClientList(key, new RedisClient(connectionString), connectionString);
+            RedisClients.Add(ctx);
+            if (defaultClient)
+            {
+                SetDefaultContextType(key);
+            }
+            return ctx.Client;
+        }
+
+        /// <summary>
+        /// 获取指定的连接
+        /// </summary>
+        /// <param name="key"></param>
+        /// <returns></returns>
+        /// <exception cref="Exception"></exception>
+        public static RedisClient Context(string key)
+        {
+            var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
+            if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
+            return ctx.Client;
+        }
+
+        /// <summary>
+        /// 检查Redis中是否有对应key且value不为空
+        /// </summary>
+        /// <param name="redisClient">redis连接</param>
+        /// <param name="key">要检查的Key</param>
+        /// <returns>
+        /// 1.key不存在,创建这个key value默认为三个空格符。返回null
+        /// 2.key存在单value为空 返回null
+        /// 3.key存在value不为空 返回获取到的值
+        /// </returns>
+        public static string? Check(this RedisClient redisClient, string key)
+        {
+            var result = redisClient.Get(key);
+            if (!string.IsNullOrEmpty(result)) return result;
+            redisClient.Set(key, "   ");
+            return null;
+        }
+    }
+}

+ 1 - 3
LogHelper/InfoLog.cs

@@ -1,6 +1,4 @@
-using System.Collections.Generic;
-
-namespace WCS.Core.Log
+namespace LogHelper
 {
     /// <summary>
     /// 信息日志

+ 1 - 1
LogHelper/LogConfigModel.cs

@@ -1,6 +1,6 @@
 using System.Collections.Generic;
 
-namespace WCS.Core.Log
+namespace LogHelper
 {
     /// <summary>
     /// 日志配置实体对象

+ 4 - 8
LogHelper/LogHelper.cs

@@ -1,15 +1,11 @@
 using log4net;
 using log4net.Config;
 using log4net.Repository;
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
 using System.Text;
 using System.Timers;
 using System.Xml;
 
-namespace WCS.Core.Log
+namespace LogHelper
 {
     /// <summary>
     /// 日志帮助类
@@ -158,7 +154,7 @@ namespace WCS.Core.Log
         private static bool _cleanSet = false;
         private static string? _previousCleanDay = null;
         private static int _cleanDays = 30;
-        private static Timer? _logCleanTimer;
+        private static System.Timers.Timer? _logCleanTimer;
 
         /// <summary>
         /// 设置配置信息
@@ -272,7 +268,7 @@ namespace WCS.Core.Log
             _cleanSet = true;
             _previousCleanDay = DateTime.Now.ToString("yyyyMMdd");
             // clean log
-            _logCleanTimer = new Timer(1000 * 60);
+            _logCleanTimer = new System.Timers.Timer(1000 * 60);
             _logCleanTimer.Elapsed += LogCleanTimer_Elapsed;
             _logCleanTimer.Enabled = true;
         }
@@ -340,7 +336,7 @@ namespace WCS.Core.Log
             }
         }
 
-        private static void LogCleanTimer_Elapsed(object sender, ElapsedEventArgs e)
+        private static void LogCleanTimer_Elapsed(object? sender, ElapsedEventArgs e)
         {
             try
             {

+ 4 - 0
LogHelper/LogHelper.csproj

@@ -6,4 +6,8 @@
     <Nullable>enable</Nullable>
   </PropertyGroup>
 
+  <ItemGroup>
+    <PackageReference Include="log4net" Version="2.0.15" />
+  </ItemGroup>
+
 </Project>

+ 1 - 1
LogHelper/LogType.cs

@@ -1,6 +1,6 @@
 using System.Collections.Generic;
 
-namespace WCS.Core.Log
+namespace LogHelper
 {
     /// <summary>
     /// 日志类型接口