RedisConnectionManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Concurrent;
  3. using StackExchange.Redis;
  4. namespace Masuit.Tools.Core.NoSQL
  5. {
  6. /// <summary>
  7. /// ConnectionMultiplexer对象管理帮助类
  8. /// </summary>
  9. public static class RedisConnectionManager
  10. {
  11. /// <summary>
  12. /// Redis服务器连接字符串,默认为:127.0.0.1:6379,allowadmin=true<br/>
  13. /// </summary>
  14. public static string RedisConnectionString
  15. {
  16. get => "127.0.0.1:6379,allowadmin=true";
  17. set { }
  18. }
  19. private static readonly object Locker = new object();
  20. private static ConnectionMultiplexer _instance;
  21. private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();
  22. /// <summary>
  23. /// 单例获取
  24. /// </summary>
  25. public static ConnectionMultiplexer Instance
  26. {
  27. get
  28. {
  29. if (_instance != null) return _instance;
  30. lock (Locker)
  31. {
  32. if (_instance == null || !_instance.IsConnected)
  33. {
  34. _instance = GetManager();
  35. }
  36. }
  37. return _instance;
  38. }
  39. }
  40. /// <summary>
  41. /// 缓存获取
  42. /// </summary>
  43. /// <param name="connectionString">连接字符串</param>
  44. /// <returns>连接对象</returns>
  45. public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString)
  46. {
  47. if (!ConnectionCache.ContainsKey(connectionString))
  48. {
  49. ConnectionCache[connectionString] = GetManager(connectionString);
  50. }
  51. return ConnectionCache[connectionString];
  52. }
  53. private static ConnectionMultiplexer GetManager(string connectionString = null)
  54. {
  55. connectionString = connectionString ?? RedisConnectionString;
  56. var connect = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(connectionString, true));
  57. //注册如下事件
  58. connect.ConnectionFailed += MuxerConnectionFailed;
  59. connect.ConnectionRestored += MuxerConnectionRestored;
  60. connect.ErrorMessage += MuxerErrorMessage;
  61. connect.ConfigurationChanged += MuxerConfigurationChanged;
  62. connect.HashSlotMoved += MuxerHashSlotMoved;
  63. connect.InternalError += MuxerInternalError;
  64. return connect;
  65. }
  66. #region 事件
  67. /// <summary>
  68. /// 配置更改时
  69. /// </summary>
  70. /// <param name="sender">触发者</param>
  71. /// <param name="e">事件参数</param>
  72. private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
  73. {
  74. Console.WriteLine("Configuration changed: " + e.EndPoint);
  75. }
  76. /// <summary>
  77. /// 发生错误时
  78. /// </summary>
  79. /// <param name="sender"></param>
  80. /// <param name="e"></param>
  81. private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
  82. {
  83. Console.WriteLine("ErrorMessage: " + e.Message);
  84. }
  85. /// <summary>
  86. /// 重新建立连接之前的错误
  87. /// </summary>
  88. /// <param name="sender"></param>
  89. /// <param name="e"></param>
  90. private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
  91. {
  92. Console.WriteLine("ConnectionRestored: " + e.EndPoint);
  93. }
  94. /// <summary>
  95. /// 连接失败 , 如果重新连接成功你将不会收到这个通知
  96. /// </summary>
  97. /// <param name="sender"></param>
  98. /// <param name="e"></param>
  99. private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
  100. {
  101. Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
  102. }
  103. /// <summary>
  104. /// 更改集群
  105. /// </summary>
  106. /// <param name="sender"></param>
  107. /// <param name="e"></param>
  108. private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
  109. {
  110. Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
  111. }
  112. /// <summary>
  113. /// redis类库错误
  114. /// </summary>
  115. /// <param name="sender"></param>
  116. /// <param name="e"></param>
  117. private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
  118. {
  119. Console.WriteLine("InternalError:Message" + e.Exception.Message);
  120. }
  121. #endregion 事件
  122. }
  123. }