CoreRedisHelper.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using CSRedis;
  2. using System.Collections.Concurrent;
  3. using System.Linq;
  4. namespace Fuel.Infrastructure
  5. {
  6. /// <summary>
  7. /// redis 多个database 客户端
  8. /// </summary>
  9. public class CoreRedisHelper
  10. {
  11. public static ConcurrentDictionary<string, CSRedisClient> redisHelpers
  12. {
  13. get;
  14. private set;
  15. }
  16. public static bool AddClient(string clientName, CSRedisClient redisHelper)
  17. {
  18. if (redisHelpers == null)
  19. {
  20. redisHelpers = new ConcurrentDictionary<string, CSRedisClient>();
  21. }
  22. if (string.IsNullOrWhiteSpace(clientName))
  23. {
  24. return false;
  25. }
  26. return redisHelpers.TryAdd(clientName, redisHelper);
  27. }
  28. /// <summary>
  29. /// 获取redis客户端名称
  30. /// </summary>
  31. /// <param name="clientName"></param>
  32. /// <returns></returns>
  33. public static CSRedisClient GetRedisHelper(string clientName)
  34. {
  35. if (redisHelpers.Any())
  36. {
  37. if (redisHelpers.Keys.Any(p => p == clientName))
  38. {
  39. return redisHelpers[clientName];
  40. }
  41. }
  42. return null;
  43. }
  44. }
  45. }