RedisDbconn.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System.Collections.Generic;
  2. using Library;
  3. using System.Linq;
  4. namespace MySystem
  5. {
  6. public class RedisDbconn
  7. {
  8. public readonly static RedisDbconn Instance = new RedisDbconn();
  9. public static CSRedis.CSRedisClient csredis;
  10. private RedisDbconn()
  11. {
  12. }
  13. #region 设置单个字段
  14. public bool Set(string key, object value)
  15. {
  16. return csredis.Set(key, value);
  17. // return false;
  18. }
  19. #endregion
  20. #region 整数累加
  21. public long AddInt(string key, long value = 1)
  22. {
  23. return csredis.IncrBy(key, value);
  24. // return 0;
  25. }
  26. #endregion
  27. #region 数字累加
  28. public decimal AddNumber(string key, decimal value = 1)
  29. {
  30. return csredis.IncrByFloat(key, value);
  31. // return 0;
  32. }
  33. #endregion
  34. #region 获取单个字段
  35. public T Get<T>(string key)
  36. {
  37. return csredis.Get<T>(key);
  38. }
  39. #endregion
  40. #region 设置散列字段
  41. public bool HSet(string key, string field, object value)
  42. {
  43. return csredis.HSet(key, field, value);
  44. // return false;
  45. }
  46. #endregion
  47. #region 散列整数累加
  48. public long HAddInt(string key, string field, long value = 1)
  49. {
  50. return csredis.HIncrBy(key, field, value);
  51. // return 0;
  52. }
  53. #endregion
  54. #region 散列数字累加
  55. public decimal HAddNumber(string key, string field, decimal value = 1)
  56. {
  57. return csredis.HIncrByFloat(key, field, value);
  58. // return 0;
  59. }
  60. #endregion
  61. #region 获取散列元素
  62. public T HGet<T>(string key, string field)
  63. {
  64. return csredis.HGet<T>(key, field);
  65. }
  66. #endregion
  67. #region 获取散列所有元素
  68. public Dictionary<string, T> HGetAll<T>(string key)
  69. {
  70. return csredis.HGetAll<T>(key);
  71. }
  72. #endregion
  73. #region 添加列表对象
  74. public long AddList(string key, object value)
  75. {
  76. return csredis.LPush(key, value);
  77. // return 0;
  78. }
  79. public long AddRightList(string key, object value)
  80. {
  81. return csredis.RPush(key, value);
  82. // return 0;
  83. }
  84. public long AddList(string key, object[] value)
  85. {
  86. return csredis.LPush(key, value);
  87. // return 0;
  88. }
  89. public bool SetList(string key, long index, object value)
  90. {
  91. return csredis.LSet(key, index, value);
  92. // return 0;
  93. }
  94. public T RPop<T>(string key)
  95. {
  96. return csredis.RPop<T>(key);
  97. }
  98. #endregion
  99. #region 移动列表元素位置
  100. public T RPopLPush<T>(string key, string tokey)
  101. {
  102. return csredis.RPopLPush<T>(key, tokey);
  103. }
  104. #endregion
  105. #region 添加集合对象
  106. public long SAdd(string key, object value)
  107. {
  108. return csredis.SAdd(key, value);
  109. // return 0;
  110. }
  111. public long SAdd(string key, object[] value)
  112. {
  113. return csredis.SAdd(key, value);
  114. // return 0;
  115. }
  116. #endregion
  117. #region 获取集合对象
  118. public T[] SGetList<T>(string key)
  119. {
  120. return csredis.SMembers<T>(key);
  121. }
  122. #endregion
  123. #region 获取列表
  124. public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
  125. {
  126. int start = (pageNum - 1) * pageSize;
  127. int end = start + pageSize - 1;
  128. T[] list = csredis.LRange<T>(key, start, end);
  129. return list.ToList();
  130. }
  131. #endregion
  132. #region 移除列表对象
  133. public long RemoveFromList(string key, object value, int count = 1)
  134. {
  135. return csredis.LRem(key, count, value);
  136. }
  137. #endregion
  138. #region 删除列表元素
  139. public void DelList<T>(string key, T item)
  140. {
  141. List<T> oldlist = GetList<T>(key, 1, 10000000);
  142. oldlist.Remove(item);
  143. Clear(key);
  144. AddList(key, oldlist.ToArray());
  145. }
  146. #endregion
  147. #region 添加排序列表对象
  148. public long AddSort(string key, object value, decimal score)
  149. {
  150. return csredis.ZAdd(key, (score, value));
  151. // return 0;
  152. }
  153. #endregion
  154. #region 获取排序列表
  155. public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
  156. {
  157. int start = (pageNum - 1) * pageSize;
  158. int end = start + pageSize;
  159. string[] list = csredis.ZRangeByScore(key, start, end);
  160. List<T> lists = new List<T>();
  161. foreach (string record in list)
  162. {
  163. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  164. }
  165. return lists;
  166. }
  167. public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
  168. {
  169. int start = (pageNum - 1) * pageSize;
  170. int end = start + pageSize;
  171. string[] list = csredis.ZRevRangeByScore(key, start, end);
  172. List<T> lists = new List<T>();
  173. foreach (string record in list)
  174. {
  175. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  176. }
  177. return lists;
  178. }
  179. #endregion
  180. public bool Remove(string key, long start, long end)
  181. {
  182. return csredis.LTrim(key, start, end);
  183. }
  184. public bool RemoveTop(string key, long count)
  185. {
  186. return RedisDbconn.Instance.Remove(key, count, RedisDbconn.Instance.Count(key) - 1);;
  187. }
  188. public long Count(string key)
  189. {
  190. return csredis.LLen(key);
  191. }
  192. public void Clear(string pattern)
  193. {
  194. string[] keys = csredis.Keys(pattern);
  195. csredis.Del(keys);
  196. }
  197. public string[] GetKeys(string pattern)
  198. {
  199. string[] keys = csredis.Keys(pattern);
  200. return keys;
  201. }
  202. public void SetExpire(string key, int expire)
  203. {
  204. csredis.Expire(key, expire); //秒为单位
  205. }
  206. #region 判断Key是否存在
  207. public bool CheckKey(string key)
  208. {
  209. if (csredis.Exists(key))
  210. {
  211. return true;
  212. }
  213. return false;
  214. }
  215. #endregion
  216. }
  217. }