RedisHub.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using FreeRedis;
  2. using ServiceCenter.Logs;
  3. namespace ServiceCenter.Redis
  4. {
  5. /// <summary>
  6. /// Redis
  7. /// </summary>
  8. public class RedisHub
  9. {
  10. /// <summary>
  11. /// 连接集合
  12. /// </summary>
  13. private static List<RedisClientList> RedisClients = new List<RedisClientList>();
  14. /// <summary>
  15. /// 默认上下文类类型
  16. /// </summary>
  17. public static string DefaultContextType { get; private set; } = null!;
  18. /// <summary>
  19. /// 默认监控上下文类类型
  20. /// </summary>
  21. public static string MonitorContextType { get; private set; } = null!;
  22. /// <summary>
  23. /// 默认调试上下文类类型
  24. /// </summary>
  25. public static string DebugContextType { get; private set; } = null!;
  26. /// <summary>
  27. /// 默认WMS上下文类类型
  28. /// </summary>
  29. public static string WMSContextType { get; private set; } = null!;
  30. /// <summary>
  31. /// 设置默认链接
  32. /// </summary>
  33. /// <param name="key"></param>
  34. public static void SetDefaultContextType(string key)
  35. {
  36. DefaultContextType = key;
  37. }
  38. /// <summary>
  39. /// 设置监控链接
  40. /// </summary>
  41. /// <param name="key"></param>
  42. public static void SetMonitorContextType(string key)
  43. {
  44. MonitorContextType = key;
  45. }
  46. /// <summary>
  47. /// 设置调试链接
  48. /// </summary>
  49. /// <param name="key"></param>
  50. public static void SetDebugContextType(string key)
  51. {
  52. DebugContextType = key;
  53. }
  54. /// <summary>
  55. /// 设置WMS链接
  56. /// </summary>
  57. /// <param name="key"></param>
  58. public static void SetWMSContextType(string key)
  59. {
  60. WMSContextType = key;
  61. }
  62. /// <summary>
  63. /// 默认链接
  64. /// </summary>
  65. public static RedisClient Default
  66. {
  67. get
  68. {
  69. {
  70. if (DefaultContextType == null)
  71. throw new Exception("请先设置默认RedisDB库,调用静态方法SetDefaultDbContextType()");
  72. return Context(DefaultContextType);
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// 监控连接
  78. /// </summary>
  79. public static RedisClient Monitor
  80. {
  81. get
  82. {
  83. {
  84. if (MonitorContextType == null)
  85. throw new Exception("请先设置监控RedisDB库,调用静态方法SetMonitorContextType()");
  86. return Context(MonitorContextType);
  87. }
  88. }
  89. }
  90. /// <summary>
  91. ///调试连接
  92. /// </summary>
  93. public static RedisClient Debug
  94. {
  95. get
  96. {
  97. {
  98. if (MonitorContextType == null)
  99. throw new Exception("请先设置DebugRedisDB库,调用静态方法SetDebugContextType()");
  100. return Context(MonitorContextType);
  101. }
  102. }
  103. }
  104. /// <summary>
  105. ///调试连接
  106. /// </summary>
  107. public static RedisClient WMS
  108. {
  109. get
  110. {
  111. {
  112. if (WMSContextType == null) throw new Exception("请先设置WMSRedisDB库,调用静态方法SetWMSContextTypee()");
  113. return Context(WMSContextType);
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// 创建一个连接
  119. /// </summary>
  120. /// <param name="connectionString"> 连接字符串</param>
  121. /// <param name="key">标识</param>
  122. /// <param name="defaultClient"> 是否为默认连接,默认为false</param>
  123. /// <returns></returns>
  124. public static RedisClient CreateContext(string connectionString, string key, bool defaultClient = false)
  125. {
  126. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  127. if (ctx != null) return ctx.Client;
  128. ctx = new RedisClientList(key, new RedisClient(connectionString), connectionString);
  129. RedisClients.Add(ctx);
  130. if (defaultClient)
  131. {
  132. SetDefaultContextType(key);
  133. }
  134. return ctx.Client;
  135. }
  136. /// <summary>
  137. /// 获取指定的连接
  138. /// </summary>
  139. /// <param name="key"></param>
  140. /// <returns></returns>
  141. /// <exception cref="Exception"></exception>
  142. public static RedisClient Context(string key)
  143. {
  144. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  145. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  146. return ctx.Client;
  147. }
  148. /// <summary>
  149. /// 执行并发锁
  150. /// 使用默认连接
  151. /// </summary>
  152. /// <param name="lockKey">并发锁key</param>
  153. /// <param name="act">执行内容</param>
  154. /// <exception cref="Exception"></exception>
  155. public static void Do(string lockKey, Action<RedisClient> act) => Do(DefaultContextType, lockKey, act);
  156. /// <summary>
  157. /// 执行并发锁
  158. /// 指定redis
  159. /// </summary>
  160. /// <param name="key">连接key</param>
  161. /// <param name="lockKey">并发锁key</param>
  162. /// <param name="act">执行内容</param>
  163. /// <exception cref="Exception"></exception>
  164. public static void Do(string key, string lockKey, Action<RedisClient> act)
  165. {
  166. if (!RedisClients.Any()) throw new Exception($"请调用{nameof(CreateContext)}方法设置设置数据库连接");
  167. var redis = Context(key);
  168. try
  169. {
  170. if (redis.Exists(lockKey)) throw new Exception($"重复调用:{lockKey}");
  171. redis.Expire(lockKey, 60);
  172. act(redis);
  173. }
  174. catch (Exception ex)
  175. {
  176. throw new KnownException($"{ex.Message}", LogLevelEnum.Mid);
  177. }
  178. finally
  179. {
  180. redis.Del(lockKey);
  181. }
  182. }
  183. }
  184. }