RedisHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Extensions.Configuration;
  7. using Newtonsoft.Json;
  8. using StackExchange.Redis;
  9. namespace IoTIntegrationPlatform.Common
  10. {
  11. /// <summary>
  12. /// redis 帮助类
  13. /// </summary>
  14. public class RedisHelper
  15. {
  16. private ConnectionMultiplexer redis;
  17. private static string redisConnectStr = string.Empty;
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. /// <param name="conn"></param>
  22. public static void SetRedisConnection(string conn)
  23. {
  24. redisConnectStr = conn;
  25. }
  26. private static RedisHelper _Instance;
  27. /// <summary>
  28. /// redis单例
  29. /// </summary>
  30. public static RedisHelper Instance
  31. {
  32. get
  33. {
  34. lock (redisConnectStr)
  35. {
  36. if (_Instance == null)
  37. {
  38. if (string.IsNullOrWhiteSpace(redisConnectStr))
  39. {
  40. Logging.Warn("redisConnectStr 未配置");
  41. }
  42. _Instance = new RedisHelper
  43. {
  44. redis = ConnectionMultiplexer.Connect(redisConnectStr)
  45. };
  46. }
  47. return _Instance;
  48. }
  49. }
  50. }
  51. public bool Remove(string key)
  52. {
  53. IDatabase db = redis.GetDatabase();
  54. return db.KeyDelete(key);
  55. }
  56. #region string
  57. /// <summary>
  58. /// 保存字符串
  59. /// </summary>
  60. /// <param name="key"></param>
  61. /// <param name="value"></param>
  62. /// <param name="TimeSpanSeconds"></param>
  63. /// <returns></returns>
  64. public bool StringSet(string key, string value, int TimeSpanSeconds = 0)
  65. {
  66. IDatabase db = redis.GetDatabase();
  67. if (TimeSpanSeconds > 0)
  68. {
  69. return db.StringSet(key, value, TimeSpan.FromSeconds(TimeSpanSeconds));
  70. }
  71. return db.StringSet(key, value);
  72. }
  73. public bool StringSet(string key, string value, TimeSpan? timeSpan)
  74. {
  75. IDatabase db = redis.GetDatabase();
  76. if (timeSpan != null)
  77. {
  78. return db.StringSet(key, value, timeSpan);
  79. }
  80. return db.StringSet(key, value);
  81. }
  82. /// <summary>
  83. /// 保存实体字符串
  84. /// </summary>
  85. /// <typeparam name="T"></typeparam>
  86. /// <param name="key"></param>
  87. /// <param name="value"></param>
  88. /// <returns></returns>
  89. public bool StringSet<T>(string key, T value, int TimeSpanSeconds = 0)
  90. {
  91. IDatabase db = redis.GetDatabase();
  92. if (TimeSpanSeconds > 0)
  93. {
  94. return db.StringSet(key, JsonConvert.SerializeObject(value), TimeSpan.FromSeconds(TimeSpanSeconds));
  95. }
  96. return db.StringSet(key, JsonConvert.SerializeObject(value));
  97. }
  98. /// <summary>
  99. /// 获取字符串
  100. /// </summary>
  101. /// <param name="key"></param>
  102. /// <returns></returns>
  103. public string StringGet(string key)
  104. {
  105. IDatabase db = redis.GetDatabase();
  106. return db.StringGet(key);
  107. }
  108. /// <summary>
  109. /// 获取实体
  110. /// </summary>
  111. /// <typeparam name="T"></typeparam>
  112. /// <param name="key"></param>
  113. /// <returns></returns>
  114. public T StringGet<T>(string key, int TimeSpanSeconds = 0)
  115. {
  116. string result = StringGet(key);
  117. if (!string.IsNullOrWhiteSpace(result))
  118. {
  119. if (TimeSpanSeconds > 0)
  120. {
  121. StringSet(key, result, TimeSpanSeconds);
  122. }
  123. return JsonConvert.DeserializeObject<T>(result);
  124. }
  125. return default;
  126. }
  127. #endregion string
  128. #region List
  129. public long ListRightPush(string key, string value)
  130. {
  131. IDatabase db = redis.GetDatabase();
  132. return db.ListRightPush(key, value);
  133. }
  134. public string ListLeftPop(string key)
  135. {
  136. IDatabase db = redis.GetDatabase();
  137. return db.ListLeftPop(key);
  138. }
  139. public static List<T> GetList<T>(string key)
  140. {
  141. List<T> taskList = new List<T>();
  142. //获取redis保存数据
  143. var redisList = RedisHelper.Instance.StringGet(key);
  144. if (!string.IsNullOrEmpty(redisList))
  145. {
  146. taskList = JsonTools.JsonToT<List<T>>(redisList);
  147. }
  148. return taskList;
  149. }
  150. #endregion List
  151. }
  152. }