123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Collections.Concurrent;
- using StackExchange.Redis;
- namespace Masuit.Tools.Core.NoSQL
- {
- /// <summary>
- /// ConnectionMultiplexer对象管理帮助类
- /// </summary>
- public static class RedisConnectionHelp
- {
- /// <summary>
- /// Redis服务器连接字符串,默认为:127.0.0.1:6379,allowadmin=true,如:<br/>
- /// </summary>
- public static string RedisConnectionString => "127.0.0.1:6379,allowadmin=true";
- private static readonly object Locker = new object();
- private static ConnectionMultiplexer _instance;
- private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();
- /// <summary>
- /// 单例获取
- /// </summary>
- public static ConnectionMultiplexer Instance
- {
- get
- {
- if (_instance != null) return _instance;
- lock (Locker)
- {
- if (_instance == null || !_instance.IsConnected)
- {
- _instance = GetManager();
- }
- }
- return _instance;
- }
- }
- /// <summary>
- /// 缓存获取
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <returns>连接对象</returns>
- public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString)
- {
- if (!ConnectionCache.ContainsKey(connectionString))
- {
- ConnectionCache[connectionString] = GetManager(connectionString);
- }
- return ConnectionCache[connectionString];
- }
- private static ConnectionMultiplexer GetManager(string connectionString = null)
- {
- connectionString = connectionString ?? RedisConnectionString;
- var connect = ConnectionMultiplexer.Connect(connectionString);
- //注册如下事件
- connect.ConnectionFailed += MuxerConnectionFailed;
- connect.ConnectionRestored += MuxerConnectionRestored;
- connect.ErrorMessage += MuxerErrorMessage;
- connect.ConfigurationChanged += MuxerConfigurationChanged;
- connect.HashSlotMoved += MuxerHashSlotMoved;
- connect.InternalError += MuxerInternalError;
- return connect;
- }
- #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);
- }
- #endregion 事件
- }
- }
|