RedisConnectionHelp.cs 4.7 KB

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