RedisHub.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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) throw new Exception("请先设置监控RedisDB库,调用静态方法SetMonitorContextType()");
  85. return Context(MonitorContextType);
  86. }
  87. }
  88. }
  89. /// <summary>
  90. ///调试连接
  91. /// </summary>
  92. public static RedisClient Debug
  93. {
  94. get
  95. {
  96. {
  97. if (MonitorContextType == null)
  98. throw new Exception("请先设置DebugRedisDB库,调用静态方法SetDebugContextType()");
  99. return Context(MonitorContextType);
  100. }
  101. }
  102. }
  103. /// <summary>
  104. ///调试连接
  105. /// </summary>
  106. public static RedisClient WMS
  107. {
  108. get
  109. {
  110. {
  111. if (WMSContextType == null) throw new Exception("请先设置WMSRedisDB库,调用静态方法SetWMSContextTypee()");
  112. return Context(WMSContextType);
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 创建一个连接
  118. /// </summary>
  119. /// <param name="connectionString"> 连接字符串</param>
  120. /// <param name="key">标识</param>
  121. /// <param name="defaultClient"> 是否为默认连接,默认为false</param>
  122. /// <returns></returns>
  123. public static RedisClient CreateContext(string connectionString, string key, bool defaultClient = false)
  124. {
  125. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  126. if (ctx != null) return ctx.Client;
  127. ctx = new RedisClientList(key, new RedisClient(connectionString), connectionString);
  128. RedisClients.Add(ctx);
  129. if (defaultClient)
  130. {
  131. SetDefaultContextType(key);
  132. }
  133. return ctx.Client;
  134. }
  135. /// <summary>
  136. /// 获取指定的连接
  137. /// </summary>
  138. /// <param name="key"></param>
  139. /// <returns></returns>
  140. /// <exception cref="Exception"></exception>
  141. public static RedisClient Context(string key)
  142. {
  143. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  144. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  145. return ctx.Client;
  146. }
  147. /// <summary>
  148. /// 执行并发锁
  149. /// 使用默认连接
  150. /// </summary>
  151. /// <param name="lockKey">并发锁key</param>
  152. /// <param name="act">执行内容</param>
  153. /// <exception cref="Exception"></exception>
  154. public static void Do(string lockKey, Action<RedisClient> act) => Do(DefaultContextType, lockKey, act);
  155. /// <summary>
  156. /// 执行并发锁
  157. /// 指定redis
  158. /// </summary>
  159. /// <param name="key">连接key</param>
  160. /// <param name="lockKey">并发锁key</param>
  161. /// <param name="act">执行内容</param>
  162. /// <exception cref="Exception"></exception>
  163. public static void Do(string key, string lockKey, Action<RedisClient> act)
  164. {
  165. if (!RedisClients.Any()) throw new Exception($"请调用{nameof(CreateContext)}方法设置设置数据库连接");
  166. var redis = Context(key);
  167. lockKey = "ConcurrentLock:" + lockKey;
  168. try
  169. {
  170. if (redis.Exists(lockKey)) throw new Exception($"重复调用:{lockKey}");
  171. redis.Set(lockKey, lockKey);
  172. redis.Expire(lockKey, 60);
  173. act(redis);
  174. }
  175. catch (Exception ex)
  176. {
  177. throw new KnownException($"{ex.Message}", LogLevelEnum.Mid);
  178. }
  179. finally
  180. {
  181. redis.Del(lockKey);
  182. }
  183. }
  184. }
  185. }