RedisHub.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using FreeRedis;
  2. namespace DBHelper.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. /// 设置默认链接
  27. /// </summary>
  28. /// <param name="key"></param>
  29. public static void SetDefaultContextType(string key)
  30. {
  31. DefaultContextType = key;
  32. }
  33. /// <summary>
  34. /// 设置监控链接
  35. /// </summary>
  36. /// <param name="key"></param>
  37. public static void SetMonitorContextType(string key)
  38. {
  39. MonitorContextType = key;
  40. }
  41. /// <summary>
  42. /// 设置监控链接
  43. /// </summary>
  44. /// <param name="key"></param>
  45. public static void SetDebugContextType(string key)
  46. {
  47. DebugContextType = key;
  48. }
  49. /// <summary>
  50. /// 默认链接
  51. /// </summary>
  52. public static RedisClient Default
  53. {
  54. get
  55. {
  56. {
  57. if (DefaultContextType == null)
  58. throw new Exception("请先设置默认RedisDB库,调用静态方法SetDefaultDbContextType()");
  59. return Context(DefaultContextType);
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// 监控连接
  65. /// </summary>
  66. public static RedisClient Monitor
  67. {
  68. get
  69. {
  70. {
  71. if (MonitorContextType == null)
  72. throw new Exception("请先设置监控RedisDB库,调用静态方法SetMonitorContextType()");
  73. return Context(MonitorContextType);
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// 监控连接
  79. /// </summary>
  80. public static RedisClient Debug
  81. {
  82. get
  83. {
  84. {
  85. if (MonitorContextType == null)
  86. throw new Exception("请先设置DebugRedisDB库,调用静态方法SetDebugContextType()");
  87. return Context(MonitorContextType);
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// 创建一个连接
  93. /// </summary>
  94. /// <param name="connectionString"> 连接字符串</param>
  95. /// <param name="key">标识</param>
  96. /// <param name="defaultClient"> 是否为默认连接,默认为false</param>
  97. /// <returns></returns>
  98. public static RedisClient CreateContext(string connectionString, string key, bool defaultClient = false)
  99. {
  100. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  101. if (ctx != null) return ctx.Client;
  102. ctx = new RedisClientList(key, new RedisClient(connectionString), connectionString);
  103. RedisClients.Add(ctx);
  104. if (defaultClient)
  105. {
  106. SetDefaultContextType(key);
  107. }
  108. return ctx.Client;
  109. }
  110. /// <summary>
  111. /// 获取指定的连接
  112. /// </summary>
  113. /// <param name="key"></param>
  114. /// <returns></returns>
  115. /// <exception cref="Exception"></exception>
  116. public static RedisClient Context(string key)
  117. {
  118. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  119. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  120. return ctx.Client;
  121. }
  122. /// <summary>
  123. /// 检查Redis中是否有对应key且value不为空
  124. /// </summary>
  125. /// <param name="redisClient">redis连接</param>
  126. /// <param name="key">要检查的Key</param>
  127. /// <returns>
  128. /// 1.key不存在,创建这个key value默认为三个空格符。返回null
  129. /// 2.key存在单value为空 返回null
  130. /// 3.key存在value不为空 返回获取到的值
  131. /// </returns>
  132. public static string? Check(this RedisClient redisClient, string key)
  133. {
  134. var result = redisClient.Get(key);
  135. if (!string.IsNullOrEmpty(result)) return result;
  136. redisClient.Set(key, " ");
  137. return null;
  138. }
  139. }
  140. }