RedisHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 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 DefaultDbContextType { get; private set; } = null!;
  20. /// <summary>
  21. /// 设置默认链接
  22. /// </summary>
  23. /// <param name="key"></param>
  24. public static void SetDefaultDbContextType(string key)
  25. {
  26. DefaultDbContextType = key;
  27. }
  28. /// <summary>
  29. /// 默认链接
  30. /// </summary>
  31. public static RedisClient Default
  32. {
  33. get
  34. {
  35. {
  36. if (DefaultDbContextType == null)
  37. throw new Exception("请先设置默认RedisDB库,调用静态方法SetDefaultDbContextType()");
  38. return Context(DefaultDbContextType);
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// 创建一个连接
  44. /// </summary>
  45. /// <param name="connectionString"> 连接字符串</param>
  46. /// <param name="key">标识</param>
  47. /// <param name="defaultClient"> 是否为默认连接,默认为false</param>
  48. /// <returns></returns>
  49. public static RedisClient CreateContext(string connectionString, string key, bool defaultClient = false)
  50. {
  51. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  52. if (ctx != null) return ctx.Client;
  53. ctx = new RedisClientList(key, new RedisClient(connectionString), connectionString);
  54. RedisClients.Add(ctx);
  55. if (defaultClient)
  56. {
  57. SetDefaultDbContextType(key);
  58. }
  59. return ctx.Client;
  60. }
  61. /// <summary>
  62. /// 获取指定的连接
  63. /// </summary>
  64. /// <param name="key"></param>
  65. /// <returns></returns>
  66. /// <exception cref="Exception"></exception>
  67. public static RedisClient Context(string key)
  68. {
  69. var ctx = RedisClients.FirstOrDefault(v => v.Key == key);
  70. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  71. return ctx.Client;
  72. }
  73. }
  74. /// <summary>
  75. /// 链接集合
  76. /// </summary>
  77. public class RedisClientList
  78. {
  79. /// <summary>
  80. /// 连接集合
  81. /// </summary>
  82. /// <param name="key">连接标识</param>
  83. /// <param name="client">连接</param>
  84. /// <param name="connectionConfig">连接字符串</param>
  85. public RedisClientList(string key, RedisClient client, string connectionConfig)
  86. {
  87. Key = key;
  88. Client = client;
  89. ConnectionConfig = connectionConfig;
  90. }
  91. /// <summary>
  92. /// 连接标识
  93. /// </summary>
  94. public string Key { get; set; }
  95. /// <summary>
  96. /// 连接
  97. /// </summary>
  98. public RedisClient Client { get; set; }
  99. /// <summary>
  100. /// 连接字符串
  101. /// </summary>
  102. public string ConnectionConfig { get; set; }
  103. }
  104. }