using Microsoft.Extensions.Caching.StackExchangeRedis; using Microsoft.Extensions.Options; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Syc.Abp.Caching.Redis { public static class RedisConnectionManage { private static readonly object Locker = new object(); private static ConnectionMultiplexer _instance; internal static string ConfigurationOptions; internal static ConfigurationOptions Configuration; /// /// 单例获取 /// public static ConnectionMultiplexer Instance { get { if (_instance == null) { lock (Locker) { if (_instance == null || !_instance.IsConnected) { _instance = GetManager(); } } } return _instance; } } private static ConnectionMultiplexer GetManager() { var connect = ConnectionMultiplexer.Connect(ConfigurationOptions); //注册如下事件 connect.ConnectionFailed += MuxerConnectionFailed; connect.ConnectionRestored += MuxerConnectionRestored; connect.ErrorMessage += MuxerErrorMessage; connect.ConfigurationChanged += MuxerConfigurationChanged; connect.HashSlotMoved += MuxerHashSlotMoved; connect.InternalError += MuxerInternalError; return connect; } #region 事件 /// /// 配置更改时 /// /// /// private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e) { Console.WriteLine("Configuration changed: " + e.EndPoint); } /// /// 发生错误时 /// /// /// private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e) { Console.WriteLine("ErrorMessage: " + e.Message); } /// /// 重新建立连接之前的错误 /// /// /// private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e) { Console.WriteLine("ConnectionRestored: " + e.EndPoint); } /// /// 连接失败 , 如果重新连接成功你将不会收到这个通知 /// /// /// private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e) { Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message))); } /// /// 更改集群 /// /// /// private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e) { Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint); } /// /// redis类库错误 /// /// /// private static void MuxerInternalError(object sender, InternalErrorEventArgs e) { Console.WriteLine("InternalError:Message" + e.Exception.Message); } #endregion 事件 } }