懒得勤快 7 년 전
부모
커밋
e254a8b15c

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <TargetFramework>netcoreapp2.0</TargetFramework>
-    <Version>1.9.5.3</Version>
+    <Version>1.9.5.4</Version>
     <Authors>懒得勤快</Authors>
     <Company>masuit.com</Company>
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。

+ 71 - 71
Masuit.Tools.Core/NoSQL/RedisHelper.cs

@@ -86,17 +86,17 @@ namespace Masuit.Tools.NoSQL
         {
             DbNum = dbNum;
             _conn = string.IsNullOrWhiteSpace(readWriteHosts) ? ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(RedisConnectionString)) : ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(readWriteHosts));
-            _conn.ConfigurationChanged += MuxerConfigurationChanged;
+            //_conn.ConfigurationChanged += MuxerConfigurationChanged;
             _conn.ConfigurationChanged += ConfigurationChanged;
-            _conn.ConnectionFailed += MuxerConnectionFailed;
+            //_conn.ConnectionFailed += MuxerConnectionFailed;
             _conn.ConnectionFailed += ConnectionFailed;
-            _conn.ConnectionRestored += MuxerConnectionRestored;
+            //_conn.ConnectionRestored += MuxerConnectionRestored;
             _conn.ConnectionRestored += ConnectionRestored;
-            _conn.ErrorMessage += MuxerErrorMessage;
+            //_conn.ErrorMessage += MuxerErrorMessage;
             _conn.ErrorMessage += ErrorMessage;
-            _conn.HashSlotMoved += MuxerHashSlotMoved;
+            //_conn.HashSlotMoved += MuxerHashSlotMoved;
             _conn.HashSlotMoved += HashSlotMoved;
-            _conn.InternalError += MuxerInternalError;
+            //_conn.InternalError += MuxerInternalError;
             _conn.InternalError += InternalError;
         }
 
@@ -110,17 +110,17 @@ namespace Masuit.Tools.NoSQL
             DbNum = dbNum;
             readWriteHosts = string.IsNullOrWhiteSpace(readWriteHosts) ? RedisConnectionString : readWriteHosts;
             _conn = ConnectionCache.GetOrAdd(readWriteHosts, ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(readWriteHosts)));
-            _conn.ConfigurationChanged += MuxerConfigurationChanged;
+            //_conn.ConfigurationChanged += MuxerConfigurationChanged;
             _conn.ConfigurationChanged += ConfigurationChanged;
-            _conn.ConnectionFailed += MuxerConnectionFailed;
+            //_conn.ConnectionFailed += MuxerConnectionFailed;
             _conn.ConnectionFailed += ConnectionFailed;
-            _conn.ConnectionRestored += MuxerConnectionRestored;
+            //_conn.ConnectionRestored += MuxerConnectionRestored;
             _conn.ConnectionRestored += ConnectionRestored;
-            _conn.ErrorMessage += MuxerErrorMessage;
+            //_conn.ErrorMessage += MuxerErrorMessage;
             _conn.ErrorMessage += ErrorMessage;
-            _conn.HashSlotMoved += MuxerHashSlotMoved;
+            //_conn.HashSlotMoved += MuxerHashSlotMoved;
             _conn.HashSlotMoved += HashSlotMoved;
-            _conn.InternalError += MuxerInternalError;
+            //_conn.InternalError += MuxerInternalError;
             _conn.InternalError += InternalError;
         }
 
@@ -1168,65 +1168,65 @@ namespace Masuit.Tools.NoSQL
 
         #region 事件
 
-        /// <summary>
-        /// 配置更改时
-        /// </summary>
-        /// <param name="sender">触发者</param>
-        /// <param name="e">事件参数</param>
-        private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
-        {
-            Console.WriteLine("Configuration changed: " + e.EndPoint);
-        }
-
-        /// <summary>
-        /// 发生错误时
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
-        {
-            Console.WriteLine("ErrorMessage: " + e.Message);
-        }
-
-        /// <summary>
-        /// 重新建立连接之前的错误
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
-        {
-            Console.WriteLine("ConnectionRestored: " + e.EndPoint);
-        }
-
-        /// <summary>
-        /// 连接失败 , 如果重新连接成功你将不会收到这个通知
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
-        {
-            Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
-        }
-
-        /// <summary>
-        /// 更改集群
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
-        {
-            Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
-        }
-
-        /// <summary>
-        /// redis类库错误
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
-        {
-            Console.WriteLine("InternalError:Message" + e.Exception.Message);
-        }
+        ///// <summary>
+        ///// 配置更改时
+        ///// </summary>
+        ///// <param name="sender">触发者</param>
+        ///// <param name="e">事件参数</param>
+        //private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
+        //{
+        //    Console.WriteLine("Configuration changed: " + e.EndPoint);
+        //}
+
+        ///// <summary>
+        ///// 发生错误时
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
+        //{
+        //    Console.WriteLine("ErrorMessage: " + e.Message);
+        //}
+
+        ///// <summary>
+        ///// 重新建立连接之前的错误
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
+        //{
+        //    Console.WriteLine("ConnectionRestored: " + e.EndPoint);
+        //}
+
+        ///// <summary>
+        ///// 连接失败 , 如果重新连接成功你将不会收到这个通知
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
+        //{
+        //    Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
+        //}
+
+        ///// <summary>
+        ///// 更改集群
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
+        //{
+        //    Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
+        //}
+
+        ///// <summary>
+        ///// redis类库错误
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
+        //{
+        //    Console.WriteLine("InternalError:Message" + e.Exception.Message);
+        //}
 
         #endregion 事件
 

+ 53 - 22
Masuit.Tools/Net/WebExtension.cs

@@ -41,7 +41,7 @@ namespace Masuit.Tools.Net
 
         #region 写Session
 
-        private static readonly RedisHelper Helper = RedisHelper.GetInstance(1);
+        //private static readonly RedisHelper Helper = RedisHelper.GetInstance(1);
         /// <summary>
         /// 写Session
         /// </summary>
@@ -83,7 +83,11 @@ namespace Masuit.Tools.Net
                 HttpCookie cookie = new HttpCookie(cookieName, sessionId);
                 HttpContext.Current.Response.Cookies.Add(cookie);
             }
-            return Helper.SetString(sessionId, obj, TimeSpan.FromMinutes(expire)); //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
+
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
+            {
+                return redisHelper.SetString(sessionId, obj, TimeSpan.FromMinutes(expire)); //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
+            }
         }
 
         /// <summary>
@@ -111,7 +115,10 @@ namespace Masuit.Tools.Net
                 HttpCookie cookie = new HttpCookie(cookieName, sessionId);
                 HttpContext.Current.Response.Cookies.Add(cookie);
             }
-            return Helper.SetString(sessionId, obj, TimeSpan.FromMinutes(expire)); //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
+            {
+                return redisHelper.SetString(sessionId, obj, TimeSpan.FromMinutes(expire)); //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
+            }
         }
 
         #endregion
@@ -146,12 +153,15 @@ namespace Masuit.Tools.Net
         /// <returns></returns> 
         public static T GetByRedis<T>(this HttpSessionState _, string key, int expire = 20) where T : class
         {
-            if (Helper.KeyExists(key))
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
             {
-                Helper.Expire(key, TimeSpan.FromMinutes(expire));
-                return Helper.GetString<T>(key);
+                if (redisHelper.KeyExists(key))
+                {
+                    redisHelper.Expire(key, TimeSpan.FromMinutes(expire));
+                    return redisHelper.GetString<T>(key);
+                }
+                return default(T);
             }
-            return default(T);
         }
 
         /// <summary>
@@ -164,12 +174,15 @@ namespace Masuit.Tools.Net
         /// <returns></returns>
         public static T GetByRedis<T>(this HttpSessionStateBase _, string key, int expire = 20) where T : class
         {
-            if (Helper.KeyExists(key))
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
             {
-                Helper.Expire(key, TimeSpan.FromMinutes(expire));
-                return Helper.GetString<T>(key);
+                if (redisHelper.KeyExists(key))
+                {
+                    redisHelper.Expire(key, TimeSpan.FromMinutes(expire));
+                    return redisHelper.GetString<T>(key);
+                }
+                return default(T);
             }
-            return default(T);
         }
 
         /// <summary>
@@ -188,10 +201,13 @@ namespace Masuit.Tools.Net
             }
             var key = HttpContext.Current.Request.Cookies[cookieName]?.Value;
             if (string.IsNullOrEmpty(key)) return default(T);
-            if (Helper.KeyExists(key))
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
             {
-                Helper.Expire(key, TimeSpan.FromMinutes(expire));
-                return Helper.GetString<T>(key);
+                if (redisHelper.KeyExists(key))
+                {
+                    redisHelper.Expire(key, TimeSpan.FromMinutes(expire));
+                    return redisHelper.GetString<T>(key);
+                }
             }
             return default(T);
         }
@@ -212,12 +228,15 @@ namespace Masuit.Tools.Net
             }
             var key = HttpContext.Current.Request.Cookies[cookieName]?.Value;
             if (string.IsNullOrEmpty(key)) return default(T);
-            if (Helper.KeyExists(key))
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
             {
-                Helper.Expire(key, TimeSpan.FromMinutes(expire));
-                return Helper.GetString<T>(key);
+                if (redisHelper.KeyExists(key))
+                {
+                    redisHelper.Expire(key, TimeSpan.FromMinutes(expire));
+                    return redisHelper.GetString<T>(key);
+                }
+                return default(T);
             }
-            return default(T);
         }
 
         /// <summary>
@@ -235,7 +254,10 @@ namespace Masuit.Tools.Net
             var key = HttpContext.Current.Request.Cookies[cookieName]?.Value;
             if (string.IsNullOrEmpty(key)) return true;
             HttpContext.Current.Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(-1);
-            return Helper.DeleteKey(key);
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
+            {
+                return redisHelper.DeleteKey(key);
+            }
         }
 
         /// <summary>
@@ -246,7 +268,10 @@ namespace Masuit.Tools.Net
         /// <returns></returns>
         public static bool RemoveByRedis(this HttpSessionStateBase _, string key = "sessionId")
         {
-            return Helper.DeleteKey(key);
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
+            {
+                return redisHelper.DeleteKey(key);
+            }
         }
 
         /// <summary>
@@ -264,7 +289,10 @@ namespace Masuit.Tools.Net
             var key = HttpContext.Current.Request.Cookies[cookieName]?.Value;
             if (string.IsNullOrEmpty(key)) return true;
             HttpContext.Current.Request.Cookies[cookieName].Expires = DateTime.Now.AddDays(-1);
-            return Helper.DeleteKey(key);
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
+            {
+                return redisHelper.DeleteKey(key);
+            }
         }
 
         /// <summary>
@@ -275,7 +303,10 @@ namespace Masuit.Tools.Net
         /// <returns></returns>
         public static bool RemoveByRedis(this HttpSessionState _, string key = "sessionId")
         {
-            return Helper.DeleteKey(key);
+            using (RedisHelper redisHelper = RedisHelper.GetInstance(1))
+            {
+                return redisHelper.DeleteKey(key);
+            }
         }
 
         #endregion

+ 71 - 71
Masuit.Tools/NoSQL/RedisHelper.cs

@@ -78,17 +78,17 @@ namespace Masuit.Tools.NoSQL
         {
             DbNum = dbNum;
             _conn = string.IsNullOrWhiteSpace(readWriteHosts) ? ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(ConfigurationManager.ConnectionStrings["RedisHosts"]?.ConnectionString ?? "127.0.0.1:6379,allowadmin=true")) : ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(readWriteHosts));
-            _conn.ConfigurationChanged += MuxerConfigurationChanged;
+            //_conn.ConfigurationChanged += MuxerConfigurationChanged;
             _conn.ConfigurationChanged += ConfigurationChanged;
-            _conn.ConnectionFailed += MuxerConnectionFailed;
+            //_conn.ConnectionFailed += MuxerConnectionFailed;
             _conn.ConnectionFailed += ConnectionFailed;
-            _conn.ConnectionRestored += MuxerConnectionRestored;
+            //_conn.ConnectionRestored += MuxerConnectionRestored;
             _conn.ConnectionRestored += ConnectionRestored;
-            _conn.ErrorMessage += MuxerErrorMessage;
+            //_conn.ErrorMessage += MuxerErrorMessage;
             _conn.ErrorMessage += ErrorMessage;
-            _conn.HashSlotMoved += MuxerHashSlotMoved;
+            //_conn.HashSlotMoved += MuxerHashSlotMoved;
             _conn.HashSlotMoved += HashSlotMoved;
-            _conn.InternalError += MuxerInternalError;
+            //_conn.InternalError += MuxerInternalError;
             _conn.InternalError += InternalError;
         }
 
@@ -102,17 +102,17 @@ namespace Masuit.Tools.NoSQL
             DbNum = dbNum;
             readWriteHosts = string.IsNullOrWhiteSpace(readWriteHosts) ? ConfigurationManager.ConnectionStrings["RedisHosts"]?.ConnectionString ?? "127.0.0.1:6379,allowadmin=true" : readWriteHosts;
             _conn = ConnectionCache.GetOrAdd(readWriteHosts, ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(readWriteHosts)));
-            _conn.ConfigurationChanged += MuxerConfigurationChanged;
+            //_conn.ConfigurationChanged += MuxerConfigurationChanged;
             _conn.ConfigurationChanged += ConfigurationChanged;
-            _conn.ConnectionFailed += MuxerConnectionFailed;
+            //_conn.ConnectionFailed += MuxerConnectionFailed;
             _conn.ConnectionFailed += ConnectionFailed;
-            _conn.ConnectionRestored += MuxerConnectionRestored;
+            //_conn.ConnectionRestored += MuxerConnectionRestored;
             _conn.ConnectionRestored += ConnectionRestored;
-            _conn.ErrorMessage += MuxerErrorMessage;
+            //_conn.ErrorMessage += MuxerErrorMessage;
             _conn.ErrorMessage += ErrorMessage;
-            _conn.HashSlotMoved += MuxerHashSlotMoved;
+            //_conn.HashSlotMoved += MuxerHashSlotMoved;
             _conn.HashSlotMoved += HashSlotMoved;
-            _conn.InternalError += MuxerInternalError;
+            //_conn.InternalError += MuxerInternalError;
             _conn.InternalError += InternalError;
         }
 
@@ -1160,65 +1160,65 @@ namespace Masuit.Tools.NoSQL
 
         #region 事件
 
-        /// <summary>
-        /// 配置更改时
-        /// </summary>
-        /// <param name="sender">触发者</param>
-        /// <param name="e">事件参数</param>
-        private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
-        {
-            Console.WriteLine("Configuration changed: " + e.EndPoint);
-        }
-
-        /// <summary>
-        /// 发生错误时
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
-        {
-            Console.WriteLine("ErrorMessage: " + e.Message);
-        }
-
-        /// <summary>
-        /// 重新建立连接之前的错误
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
-        {
-            Console.WriteLine("ConnectionRestored: " + e.EndPoint);
-        }
-
-        /// <summary>
-        /// 连接失败 , 如果重新连接成功你将不会收到这个通知
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
-        {
-            Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
-        }
-
-        /// <summary>
-        /// 更改集群
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
-        {
-            Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
-        }
-
-        /// <summary>
-        /// redis类库错误
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
-        {
-            Console.WriteLine("InternalError:Message" + e.Exception.Message);
-        }
+        ///// <summary>
+        ///// 配置更改时
+        ///// </summary>
+        ///// <param name="sender">触发者</param>
+        ///// <param name="e">事件参数</param>
+        //private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
+        //{
+        //    Console.WriteLine("Configuration changed: " + e.EndPoint);
+        //}
+
+        ///// <summary>
+        ///// 发生错误时
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
+        //{
+        //    Console.WriteLine("ErrorMessage: " + e.Message);
+        //}
+
+        ///// <summary>
+        ///// 重新建立连接之前的错误
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
+        //{
+        //    Console.WriteLine("ConnectionRestored: " + e.EndPoint);
+        //}
+
+        ///// <summary>
+        ///// 连接失败 , 如果重新连接成功你将不会收到这个通知
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
+        //{
+        //    Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
+        //}
+
+        ///// <summary>
+        ///// 更改集群
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
+        //{
+        //    Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
+        //}
+
+        ///// <summary>
+        ///// redis类库错误
+        ///// </summary>
+        ///// <param name="sender"></param>
+        ///// <param name="e"></param>
+        //private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
+        //{
+        //    Console.WriteLine("InternalError:Message" + e.Exception.Message);
+        //}
 
         #endregion 事件
 

+ 2 - 2
Masuit.Tools/Properties/AssemblyInfo.cs

@@ -36,7 +36,7 @@ using System.Runtime.InteropServices;
 // 方法是按如下所示使用“*”: :
 // [assembly: AssemblyVersion("1.0.*")]
 
-[assembly: AssemblyVersion("1.9.5.3")]
-[assembly: AssemblyFileVersion("1.9.5.3")]
+[assembly: AssemblyVersion("1.9.5.4")]
+[assembly: AssemblyFileVersion("1.9.5.4")]
 [assembly: NeutralResourcesLanguage("zh-CN")]