RedisHub.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using FreeRedis;
  2. namespace ServiceCenter.Redis
  3. {
  4. /// <summary>
  5. /// Redis
  6. /// </summary>
  7. public static class RedisHub
  8. {
  9. /// <summary>
  10. /// 连接集合
  11. /// </summary>
  12. private static List<RedisClientList> RedisClients = new List<RedisClientList>();
  13. /// <summary>
  14. /// 默认上下文类类型
  15. /// </summary>
  16. public static string DefaultContextType { get; private set; } = null!;
  17. /// <summary>
  18. /// 默认监控上下文类类型
  19. /// </summary>
  20. public static string MonitorContextType { get; private set; } = null!;
  21. /// <summary>
  22. /// 默认调试上下文类类型
  23. /// </summary>
  24. public static string DebugContextType { get; private set; } = null!;
  25. /// <summary>
  26. /// 默认WMS上下文类类型
  27. /// </summary>
  28. public static string WMSContextType { get; private set; } = null!;
  29. /// <summary>
  30. /// 设置默认链接
  31. /// </summary>
  32. /// <param name="key"></param>
  33. public static void SetDefaultContextType(string key)
  34. {
  35. DefaultContextType = key;
  36. }
  37. /// <summary>
  38. /// 设置监控链接
  39. /// </summary>
  40. /// <param name="key"></param>
  41. public static void SetMonitorContextType(string key)
  42. {
  43. MonitorContextType = key;
  44. }
  45. /// <summary>
  46. /// 设置调试链接
  47. /// </summary>
  48. /// <param name="key"></param>
  49. public static void SetDebugContextType(string key)
  50. {
  51. DebugContextType = key;
  52. }
  53. /// <summary>
  54. /// 设置WMS链接
  55. /// </summary>
  56. /// <param name="key"></param>
  57. public static void SetWMSContextType(string key)
  58. {
  59. WMSContextType = key;
  60. }
  61. /// <summary>
  62. /// 默认链接
  63. /// </summary>
  64. public static RedisClient Default
  65. {
  66. get
  67. {
  68. {
  69. if (DefaultContextType == null)
  70. throw new Exception("请先设置默认RedisDB库,调用静态方法SetDefaultDbContextType()");
  71. return Context(DefaultContextType);
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// 监控连接
  77. /// </summary>
  78. public static RedisClient Monitor
  79. {
  80. get
  81. {
  82. {
  83. if (MonitorContextType == null)
  84. 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. /// 检查Redis中是否有对应key且value不为空
  149. /// </summary>
  150. /// <param name="redisClient">redis连接</param>
  151. /// <param name="key">要检查的Key</param>
  152. /// <returns>
  153. /// 1.key不存在,创建这个key value默认为三个空格符。返回null
  154. /// 2.key存在单value为空 返回null
  155. /// 3.key存在value不为空 返回获取到的值
  156. /// </returns>
  157. public static string? Check(this RedisClient redisClient, string key)
  158. {
  159. var result = redisClient.Get(key);
  160. if (!string.IsNullOrEmpty(result)) return result;
  161. redisClient.Set(key, " ");
  162. Console.WriteLine($"无{key},创建并写入默认值: ");
  163. return null;
  164. }
  165. /// <summary>
  166. /// 检查Redis中是否有对应key且value不为空
  167. /// </summary>
  168. /// <param name="redisClient">redis连接</param>
  169. /// <param name="key">要检查的Key</param>
  170. /// <param name="defaults">创建key是写入的默认值</param>
  171. /// <returns>
  172. /// 1.key不存在,创建这个key 并写入传入的默认值。返回默认值
  173. /// 2.key存在单value为空 返回null
  174. /// 3.key存在value不为空 返回获取到的值
  175. /// </returns>
  176. public static string? Check(this RedisClient redisClient, string key, string defaults)
  177. {
  178. var result = redisClient.Get(key);
  179. if (!string.IsNullOrEmpty(result)) return result;
  180. redisClient.Set(key, defaults);
  181. Console.WriteLine($"无{key},创建并写入默认值:{defaults}");
  182. return defaults;
  183. }
  184. }
  185. }