RedisDbconn.cs 6.0 KB

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