RedisConnectionManager.cs 4.4 KB

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