RedisCache.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using Newtonsoft.Json;
  2. using StackExchange.Redis;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using MqttMsgServer;
  11. namespace MqttMsgServer.Tools
  12. {
  13. public class RedisHelper: IDisposable
  14. {
  15. private readonly string _redisConnectionStr = AppSetting.GetValue("redis");
  16. private IDatabase Db { get; set; }
  17. private static Object lockObj = new Object();
  18. //public RedisHelper()
  19. //{
  20. // redis = ConnectionMultiplexer.Connect(RedisConnectionStr);
  21. // db = redis.GetDatabase();
  22. //}
  23. public static ConnectionMultiplexer Redis { get; private set; }
  24. static RedisHelper()
  25. {
  26. lock (lockObj)
  27. {
  28. Instance = new RedisHelper();
  29. }
  30. }
  31. private RedisHelper()
  32. {
  33. if (Redis == null)
  34. {
  35. Redis = ConnectionMultiplexer.Connect(_redisConnectionStr);
  36. Db = Redis.GetDatabase();
  37. }
  38. }
  39. public static RedisHelper Instance { get; }
  40. #region string类型操作
  41. /// <summary>
  42. /// set or update the value for string key
  43. /// </summary>
  44. /// <param name="key"></param>
  45. /// <param name="value"></param>
  46. /// <returns></returns>
  47. public bool SetStringValue(string key, string value)
  48. {
  49. return Db.StringSet(key, value);
  50. }
  51. /// <summary>
  52. /// 保存单个key value
  53. /// </summary>
  54. /// <param name="key">Redis Key</param>
  55. /// <param name="value">保存的值</param>
  56. /// <param name="expiry">过期时间</param>
  57. /// <returns></returns>
  58. public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
  59. {
  60. return Db.StringSet(key, value, expiry);
  61. }
  62. /// <summary>
  63. /// 保存一个对象
  64. /// </summary>
  65. /// <typeparam name="T"></typeparam>
  66. /// <param name="key"></param>
  67. /// <param name="obj"></param>
  68. /// <returns></returns>
  69. public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
  70. {
  71. //LogHelper.LogInfo(this, Marshal.SizeOf(Db));
  72. string json = JsonConvert.SerializeObject(obj);
  73. return Db.StringSet(key, json, expiry);
  74. }
  75. /// <summary>
  76. /// 获取一个key的对象
  77. /// </summary>
  78. /// <typeparam name="T"></typeparam>
  79. /// <param name="key"></param>
  80. /// <returns></returns>
  81. public T GetStringKey<T>(string key) where T : class
  82. {
  83. var res = Db.StringGet(key);
  84. if (!res.IsNull)
  85. return JsonConvert.DeserializeObject<T>(res);
  86. return null;
  87. }
  88. /// <summary>
  89. /// get the value for string key
  90. /// </summary>
  91. /// <param name="key"></param>
  92. /// <returns></returns>
  93. public string GetStringValue(string key)
  94. {
  95. return Db.StringGet(key);
  96. }
  97. /// <summary>
  98. /// Delete the value for string key
  99. /// </summary>
  100. /// <param name="key"></param>
  101. /// <returns></returns>
  102. public bool DeleteStringKey(string key)
  103. {
  104. return Db.KeyDelete(key);
  105. }
  106. #endregion
  107. #region 哈希类型操作
  108. /// <summary>
  109. /// set or update the HashValue for string key
  110. /// </summary>
  111. /// <param name="key"></param>
  112. /// <param name="hashkey"></param>
  113. /// <param name="value"></param>
  114. /// <returns></returns>
  115. public bool SetHashValue(string key, string hashkey, string value)
  116. {
  117. return Db.HashSet(key, hashkey, value);
  118. }
  119. /// <summary>
  120. /// set or update the HashValue for string key
  121. /// </summary>
  122. /// <typeparam name="T"></typeparam>
  123. /// <param name="key"></param>
  124. /// <param name="hashkey"></param>
  125. /// <param name="t">defined class</param>
  126. /// <returns></returns>
  127. public bool SetHashValue<T>(String key, string hashkey, T t) where T : class
  128. {
  129. var json = JsonConvert.SerializeObject(t);
  130. return Db.HashSet(key, hashkey, json);
  131. }
  132. /// <summary>
  133. /// 保存一个集合
  134. /// </summary>
  135. /// <typeparam name="T"></typeparam>
  136. /// <param name="key">Redis Key</param>
  137. /// <param name="list">数据集合</param>
  138. /// <param name="getModelId"></param>
  139. public void HashSet<T>(string key, List<T> list, Func<T, string> getModelId)
  140. {
  141. List<HashEntry> listHashEntry = new List<HashEntry>();
  142. foreach (var item in list)
  143. {
  144. string json = JsonConvert.SerializeObject(item);
  145. listHashEntry.Add(new HashEntry(getModelId(item), json));
  146. }
  147. Db.HashSet(key, listHashEntry.ToArray());
  148. }
  149. /// <summary>
  150. /// 获取hashkey所有的值
  151. /// </summary>
  152. /// <typeparam name="T"></typeparam>
  153. /// <param name="key"></param>
  154. /// <returns></returns>
  155. //public List<T> HashGetAll<T>(string key) where T : class
  156. //{
  157. // List<T> result = new List<T>();
  158. // HashEntry[] arr = db.HashGetAll(key);
  159. // foreach (var item in arr)
  160. // {
  161. // if (!item.Value.IsNullOrEmpty)
  162. // {
  163. // T t;
  164. // if (JsonConvert.DeserializeObject<T>(item.Value, out t))
  165. // {
  166. // result.Add(t);
  167. // }
  168. // }
  169. // }
  170. // return result;
  171. // //result =JsonHelper.DeserializeJsonToList<T>(arr.ToString());
  172. // //return result;
  173. //}
  174. /// <summary>
  175. /// get the HashValue for string key and hashkey
  176. /// </summary>
  177. /// <param name="key">Represents a key that can be stored in redis</param>
  178. /// <param name="hashkey"></param>
  179. /// <returns></returns>
  180. public RedisValue GetHashValue(string key, string hashkey)
  181. {
  182. RedisValue result = Db.HashGet(key, hashkey);
  183. return result;
  184. }
  185. /// <summary>
  186. /// get the HashValue for string key and hashkey
  187. /// </summary>
  188. /// <param name="key">Represents a key that can be stored in redis</param>
  189. /// <param name="hashkey"></param>
  190. /// <returns></returns>
  191. //public T GetHashValue<T>(string key, string hashkey) where T : class
  192. //{
  193. // RedisValue result = db.HashGet(key, hashkey);
  194. // if (string.IsNullOrEmpty(result))
  195. // {
  196. // return null;
  197. // }
  198. // T t;
  199. // if (JsonConvert.DeserializeObject<T>(result, out t))
  200. // {
  201. // return t;
  202. // }
  203. // return null;
  204. //}
  205. /// <summary>
  206. /// delete the HashValue for string key and hashkey
  207. /// </summary>
  208. /// <param name="key"></param>
  209. /// <param name="hashkey"></param>
  210. /// <returns></returns>
  211. public bool DeleteHashValue(string key, string hashkey)
  212. {
  213. return Db.HashDelete(key, hashkey);
  214. }
  215. #endregion
  216. public void Dispose()
  217. {
  218. Redis?.Dispose();
  219. }
  220. }
  221. }