RedisDbconn.cs 6.4 KB

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