RedisOption.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using Newtonsoft.Json;
  2. using ServiceStack.Redis;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace CarLocalMeter
  9. {
  10. /// <summary>
  11. /// 这个只是做个参考,我们只用前面两个
  12. /// </summary>
  13. public class RedisOption
  14. {
  15. #region 数据缓存,实际上我们只需要这个里面的3个接口即可
  16. /// <summary>
  17. /// 存储数据
  18. /// </summary>
  19. /// <param name="key">key值</param>
  20. /// <param name="value">value值</param>
  21. /// <param name="timeout">过期时间</param>
  22. /// <returns></returns>
  23. public bool setRedis(string key, object value, int timeout = 0)
  24. {
  25. try
  26. {
  27. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  28. {
  29. if (client == null) return false;
  30. if (client.ContainsKey(key))
  31. client.Replace<object>(key, value);
  32. else
  33. client.Set<object>(key, value);
  34. if (timeout > 0)
  35. client.ExpireEntryIn(key, TimeSpan.FromSeconds(timeout));
  36. //也可以用DateTime的设置,这个随意
  37. //client.ExpireEntryAt(key,DateTime.Now.AddSeconds(timeout));
  38. return true;
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. //_logger.LogError($"========Redis写入失败:{ex.Message}===========");
  44. return false;
  45. }
  46. }
  47. /// <summary>
  48. /// 传入的T必须是能够正常转换未对应对格式的数据,
  49. /// 比如Hashtable或者Dictionary《string,string>存入的数据格式一个是类似"{'a':'1','b':'2'}"
  50. /// 如果是string或者是int之类的则 "xxx"即可
  51. /// 如果是Hashtable则读取数据方式:foreach (var mc in mm.Keys) mm[mc];
  52. /// 如果是Dictionary则读取数据方式:foreach (var mc in mm.Keys) mm[mc];
  53. /// </summary>
  54. /// <typeparam name="T"></typeparam>
  55. /// <param name="key"></param>
  56. /// <returns></returns>
  57. public T getRedisObj<T>(string key)
  58. {
  59. try
  60. {
  61. T t = default(T);
  62. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort))
  63. {
  64. if (client == null) return t;
  65. string configData = client.Get<string>(key);
  66. if (string.IsNullOrWhiteSpace(configData)) return t;
  67. return JsonConvert.DeserializeObject<T>(configData.Trim());
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. //_logger.LogError($"========Redis写入失败:{ex.Message}===========");
  73. throw new Exception("数据获取失败:" + ex.Message);
  74. }
  75. }
  76. public bool delRedisObj<T>(string key)
  77. {
  78. try
  79. {
  80. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort))
  81. {
  82. if (client == null) return false;
  83. client.Del(key);
  84. return true;
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. throw new Exception("数据删除失败:" + ex.Message);
  90. }
  91. }
  92. #endregion
  93. #region redis 的 list 数据类型对于大部分使用者来说,是实现队列服务的最经济,最简单的方式,取出后将自动删除
  94. /// <summary>
  95. /// EnqueueItemOnList是先入先出,比如写入的是1,2,3读取的时候就是1,2,3
  96. /// 读取完成后会自动删除
  97. /// </summary>
  98. /// <param name="key"></param>
  99. /// <param name="qList"></param>
  100. /// <returns></returns>
  101. public bool setEnqueue(string key, List<string> qList)
  102. {
  103. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  104. {
  105. if (client == null) return false;
  106. foreach (string t in qList)
  107. {
  108. client.EnqueueItemOnList(key, t); //入队
  109. }
  110. return true;
  111. }
  112. }
  113. /// <summary>
  114. /// PushItemToList是先入后出,比如写入了 1,2,3读取的时候是按3,2,1读取出来的
  115. /// 读取完成后会自动删除
  116. /// </summary>
  117. /// <param name="key"></param>
  118. /// <param name="qList"></param>
  119. /// <returns></returns>
  120. public bool setPushItem(string key, List<string> qList)
  121. {
  122. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  123. {
  124. if (client == null) return false;
  125. foreach (string t in qList)
  126. {
  127. client.PushItemToList(key, t); //入栈
  128. }
  129. return true;
  130. }
  131. }
  132. /// <summary>
  133. /// 读取队列中所有的数据:上面EnqueueItemOnList跟PushItemToList写入的数据
  134. /// 当然我们很多时候可能是读取1个,然后使用完后再进行读取,那么用不带s的
  135. /// </summary>
  136. /// <param name="key"></param>
  137. /// <returns></returns>
  138. public string getListDb(string key)
  139. {
  140. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  141. {
  142. if (client == null) return "";
  143. return client.DequeueItemFromList(key) ?? "";
  144. }
  145. }
  146. /// <summary>
  147. /// 读取队列中所有的数据:上面EnqueueItemOnList跟PushItemToList写入的数据
  148. /// 当然我们很多时候可能是读取1个,然后使用完后再进行读取,那么用不带s的
  149. /// </summary>
  150. /// <param name="key"></param>
  151. /// <returns></returns>
  152. public IEnumerable<string> getListDbs(string key)
  153. {
  154. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  155. {
  156. if (client == null) yield break;
  157. for (int i = 0; i < client.GetListCount(key); i++)
  158. {
  159. yield return client.DequeueItemFromList(key) ?? "";
  160. }
  161. }
  162. }
  163. #endregion
  164. #region Redis 的 Set 是 string 类型的无序集合,主要应用在一些需要求交集、并集、补集这样的场景
  165. /// <summary>
  166. /// Set集合中key值可以是相同的,value值不允许重复
  167. /// </summary>
  168. /// <param name="key"></param>
  169. /// <param name="value"></param>
  170. /// <returns></returns>
  171. public bool setRedisItem(string key, string value)
  172. {
  173. try
  174. {
  175. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  176. {
  177. if (client == null) return false;
  178. client.AddItemToSet(key,value);
  179. return true;
  180. }
  181. #region 利用集合可求出两集合相同的value的值
  182. /*
  183. #region Set操作
  184. client.AddItemToSet("QQ用户1", "好友A");
  185. client.AddItemToSet("QQ用户1", "好友B");
  186. client.AddItemToSet("QQ用户2", "好友B");
  187. client.AddItemToSet("QQ用户2", "好友F");
  188. var setunion = client.GetIntersectFromSets("QQ用户1", "QQ用户2");
  189. foreach (var item in setunion) Console.WriteLine($"QQ用户1和QQ用户2的共同好友为:{item}");
  190. //实际上GetIntersectFromSets取得的是HashSet,所以可以直接操作2个HashSet来取交集、并集等
  191. //*/
  192. #endregion
  193. }
  194. catch (Exception ex)
  195. {
  196. //_logger.LogError($"========Redis写入失败:{ex.Message}===========");
  197. return false;
  198. }
  199. }
  200. public HashSet<string> getRedisItem(string key)
  201. {
  202. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  203. {
  204. return client.GetIntersectFromSets(key);
  205. }
  206. }
  207. public bool setRedisItemSort(string key, string value,double db)
  208. {
  209. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  210. {
  211. if (client == null) return false;
  212. client.AddItemToSortedSet(key, value, db);
  213. return true;
  214. }
  215. }
  216. public IDictionary<string,double> getRedisItemSort(string key)
  217. {
  218. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  219. {
  220. return client.GetRangeWithScoresFromSortedSet(key, 1, 3);
  221. }
  222. }
  223. public bool setRedisHash(string hashId, string key, string value)
  224. {
  225. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  226. {
  227. if (client == null) return false;
  228. client.SetEntryInHash(hashId, key, value);
  229. return true;
  230. }
  231. }
  232. public Dictionary<string, string> getRedisHash(string hashId)
  233. {
  234. using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
  235. {
  236. if (client == null) return null;
  237. return client.GetAllEntriesFromHash(hashId);
  238. }
  239. }
  240. #endregion
  241. }
  242. }