RedisHelper.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using FreeRedis;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace WCS.Core.Redis
  6. {
  7. /// <summary>
  8. /// Redis操作类
  9. /// </summary>
  10. public static class RedisHelper
  11. {
  12. /// <summary>
  13. /// 连接集合
  14. /// </summary>
  15. private static List<RedisClientList> RedisClients = new List<RedisClientList>();
  16. /// <summary>
  17. /// 默认上下文类类型
  18. /// </summary>
  19. public static string DefaultContextType { get; private set; } = null!;
  20. /// <summary>
  21. /// 默认上下文类类型
  22. /// </summary>
  23. public static string MonitorContextType { get; private set; } = null!;
  24. /// <summary>
  25. /// 设置默认链接
  26. /// </summary>
  27. /// <param name="key"></param>
  28. public static void SetDefaultContextType(string key)
  29. {
  30. DefaultContextType = key;
  31. }
  32. /// <summary>
  33. /// 设置监控链接
  34. /// </summary>
  35. /// <param name="key"></param>
  36. public static void SetMonitorContextType(string key)
  37. {
  38. MonitorContextType = key;
  39. }
  40. /// <summary>
  41. /// 默认链接
  42. /// </summary>
  43. public static RedisClient Default
  44. {
  45. get
  46. {
  47. {
  48. if (DefaultContextType == null)
  49. throw new Exception("请先设置默认RedisDB库,调用静态方法SetDefaultDbContextType()");
  50. return Context(DefaultContextType);
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// 监控连接
  56. /// </summary>
  57. public static RedisClient Monitor
  58. {
  59. get
  60. {
  61. {
  62. if (MonitorContextType == null)
  63. throw new Exception("请先设置监控RedisDB库,调用静态方法SetMonitorContextType()");
  64. return Context(MonitorContextType);
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 创建一个连接
  70. /// </summary>
  71. /// <param name="connectionString"> 连接字符串</param>
  72. /// <param name="key">标识</param>
  73. /// <param name="defaultClient"> 是否为默认连接,默认为false</param>
  74. /// <returns></returns>
  75. public static RedisClient CreateContext(string connectionString, string key, bool defaultClient = false)
  76. {
  77. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  78. if (ctx != null) return ctx.Client;
  79. ctx = new RedisClientList(key, new RedisClient(connectionString), connectionString);
  80. RedisClients.Add(ctx);
  81. if (defaultClient)
  82. {
  83. SetDefaultContextType(key);
  84. }
  85. return ctx.Client;
  86. }
  87. /// <summary>
  88. /// 获取指定的连接
  89. /// </summary>
  90. /// <param name="key"></param>
  91. /// <returns></returns>
  92. /// <exception cref="Exception"></exception>
  93. public static RedisClient Context(string key)
  94. {
  95. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  96. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  97. return ctx.Client;
  98. }
  99. /// <summary>
  100. /// 检查Redis中是否有对应key且value不为空
  101. /// </summary>
  102. /// <param name="redisClient">redis连接</param>
  103. /// <param name="key">要检查的Key</param>
  104. /// <returns>
  105. /// 1.key不存在,创建这个key value默认为三个空格符。返回null
  106. /// 2.key存在单value为空 返回null
  107. /// 3.key存在value不为空 返回获取到的值
  108. /// </returns>
  109. public static string? Check(this RedisClient redisClient, string key)
  110. {
  111. var result = redisClient.Get(key);
  112. if (!string.IsNullOrEmpty(result)) return result;
  113. redisClient.Set(key, " ");
  114. return null;
  115. }
  116. }
  117. /// <summary>
  118. /// 链接集合
  119. /// </summary>
  120. public class RedisClientList
  121. {
  122. /// <summary>
  123. /// 连接集合
  124. /// </summary>
  125. /// <param name="key">连接标识</param>
  126. /// <param name="client">连接</param>
  127. /// <param name="connectionConfig">连接字符串</param>
  128. public RedisClientList(string key, RedisClient client, string connectionConfig)
  129. {
  130. Key = key;
  131. Client = client;
  132. ConnectionConfig = connectionConfig;
  133. }
  134. /// <summary>
  135. /// 连接标识
  136. /// </summary>
  137. public string Key { get; set; }
  138. /// <summary>
  139. /// 连接
  140. /// </summary>
  141. public RedisClient Client { get; set; }
  142. /// <summary>
  143. /// 连接字符串
  144. /// </summary>
  145. public string ConnectionConfig { get; set; }
  146. }
  147. }