RedisConnectionHelp.cs 4.9 KB

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