RedisHub.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. public static object redisLock = new object();
  31. /// <summary>
  32. /// 设置默认链接
  33. /// </summary>
  34. /// <param name="key"></param>
  35. public static void SetDefaultContextType(string key)
  36. {
  37. DefaultContextType = key;
  38. }
  39. /// <summary>
  40. /// 设置监控链接
  41. /// </summary>
  42. /// <param name="key"></param>
  43. public static void SetMonitorContextType(string key)
  44. {
  45. MonitorContextType = key;
  46. }
  47. /// <summary>
  48. /// 设置调试链接
  49. /// </summary>
  50. /// <param name="key"></param>
  51. public static void SetDebugContextType(string key)
  52. {
  53. DebugContextType = key;
  54. }
  55. /// <summary>
  56. /// 设置WMS链接
  57. /// </summary>
  58. /// <param name="key"></param>
  59. public static void SetWMSContextType(string key)
  60. {
  61. WMSContextType = key;
  62. }
  63. /// <summary>
  64. /// 默认链接
  65. /// </summary>
  66. public static RedisClient Default
  67. {
  68. get
  69. {
  70. {
  71. if (DefaultContextType == null)
  72. throw new Exception("请先设置默认RedisDB库,调用静态方法SetDefaultDbContextType()");
  73. return Context(DefaultContextType);
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// 监控连接
  79. /// </summary>
  80. public static RedisClient Monitor
  81. {
  82. get
  83. {
  84. {
  85. if (MonitorContextType == null) 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. lockKey = "ConcurrentLock:" + lockKey;
  169. try
  170. {
  171. if (redis.Exists(lockKey)) throw new Exception($"重复调用:{lockKey}");
  172. redis.Set(lockKey, lockKey);
  173. redis.Expire(lockKey, 60);
  174. act(redis);
  175. }
  176. catch (Exception ex)
  177. {
  178. throw new KnownException($"{ex.Message}", LogLevelEnum.Mid);
  179. }
  180. finally
  181. {
  182. redis.Del(lockKey);
  183. }
  184. }
  185. }
  186. }