RedisDbconn.cs 6.5 KB

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