RedisHelper.cs 3.2 KB

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