RedisConnectionHelp.cs 4.8 KB

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