RedisDbconn.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 T RPop<T>(string key)
  90. {
  91. return csredis.RPop<T>(key);
  92. }
  93. #endregion
  94. #region 添加集合对象
  95. public long SAdd(string key, object value)
  96. {
  97. return csredis.SAdd(key, value);
  98. // return 0;
  99. }
  100. public long SAdd(string key, object[] value)
  101. {
  102. return csredis.SAdd(key, value);
  103. // return 0;
  104. }
  105. #endregion
  106. #region 获取集合对象
  107. public T[] SGetList<T>(string key)
  108. {
  109. return csredis.SMembers<T>(key);
  110. }
  111. #endregion
  112. #region 修改列表对象
  113. public bool SetList(string key, int index, object value)
  114. {
  115. long itemindex = csredis.LLen(key) - index - 1;
  116. return csredis.LSet(key, itemindex, value);
  117. // return false;
  118. }
  119. #endregion
  120. #region 获取列表
  121. public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
  122. {
  123. int start = (pageNum - 1) * pageSize;
  124. int end = start + pageSize - 1;
  125. T[] list = csredis.LRange<T>(key, start, end);
  126. return list.ToList();
  127. }
  128. #endregion
  129. #region 删除列表元素
  130. public void DelList<T>(string key, T item)
  131. {
  132. List<T> oldlist = GetList<T>(key, 1, 10000000);
  133. oldlist.Remove(item);
  134. Clear(key);
  135. AddList(key, oldlist.ToArray());
  136. }
  137. #endregion
  138. #region 添加排序列表对象
  139. public long AddSort(string key, object value, decimal score)
  140. {
  141. return csredis.ZAdd(key, (score, value));
  142. // return 0;
  143. }
  144. #endregion
  145. #region 获取排序列表
  146. public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
  147. {
  148. int start = (pageNum - 1) * pageSize;
  149. int end = start + pageSize;
  150. string[] list = csredis.ZRangeByScore(key, start, end);
  151. List<T> lists = new List<T>();
  152. foreach (string record in list)
  153. {
  154. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  155. }
  156. return lists;
  157. }
  158. public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
  159. {
  160. int start = (pageNum - 1) * pageSize;
  161. int end = start + pageSize;
  162. string[] list = csredis.ZRevRangeByScore(key, start, end);
  163. List<T> lists = new List<T>();
  164. foreach (string record in list)
  165. {
  166. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  167. }
  168. return lists;
  169. }
  170. #endregion
  171. public bool Remove(string key, long start, long end)
  172. {
  173. return csredis.LTrim(key, start, end);
  174. }
  175. public bool RemoveTop(string key, long count)
  176. {
  177. return RedisDbconn.Instance.Remove(key, count, RedisDbconn.Instance.Count(key) - 1);;
  178. }
  179. public long Count(string key)
  180. {
  181. return csredis.LLen(key);
  182. }
  183. public void Clear(string pattern)
  184. {
  185. string[] keys = csredis.Keys(pattern);
  186. csredis.Del(keys);
  187. }
  188. public string[] GetKeys(string pattern)
  189. {
  190. string[] keys = csredis.Keys(pattern);
  191. return keys;
  192. }
  193. public void SetExpire(string key, int expire)
  194. {
  195. csredis.Expire(key, expire); //秒为单位
  196. }
  197. #region 判断Key是否存在
  198. public bool CheckKey(string key)
  199. {
  200. if (csredis.Exists(key))
  201. {
  202. return true;
  203. }
  204. return false;
  205. }
  206. #endregion
  207. }
  208. }