RedisDbconn.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. private RedisDbconn()
  9. {
  10. var csredis = new CSRedis.CSRedisClient(ConfigurationManager.AppSettings["RedisConnStr"].ToString());
  11. //初始化 RedisHelper
  12. RedisHelper.Initialization(csredis);
  13. }
  14. #region 设置单个字段
  15. public bool Set(string key, object value)
  16. {
  17. return RedisHelper.Set(key, value);
  18. // return false;
  19. }
  20. #endregion
  21. #region 整数累加
  22. public long AddInt(string key, long value = 1)
  23. {
  24. return RedisHelper.IncrBy(key, value);
  25. // return 0;
  26. }
  27. #endregion
  28. #region 数字累加
  29. public decimal AddNumber(string key, decimal value = 1)
  30. {
  31. return RedisHelper.IncrByFloat(key, value);
  32. // return 0;
  33. }
  34. #endregion
  35. #region 获取单个字段
  36. public T Get<T>(string key)
  37. {
  38. return RedisHelper.Get<T>(key);
  39. }
  40. #endregion
  41. #region 设置散列字段
  42. public bool HSet(string key, string field, object value)
  43. {
  44. return RedisHelper.HSet(key, field, value);
  45. // return false;
  46. }
  47. #endregion
  48. #region 散列整数累加
  49. public long HAddInt(string key, string field, long value = 1)
  50. {
  51. return RedisHelper.HIncrBy(key, field, value);
  52. // return 0;
  53. }
  54. #endregion
  55. #region 散列数字累加
  56. public decimal HAddNumber(string key, string field, decimal value = 1)
  57. {
  58. return RedisHelper.HIncrByFloat(key, field, value);
  59. // return 0;
  60. }
  61. #endregion
  62. #region 获取散列元素
  63. public T HGet<T>(string key, string field)
  64. {
  65. return RedisHelper.HGet<T>(key, field);
  66. }
  67. #endregion
  68. #region 获取散列所有元素
  69. public Dictionary<string, T> HGetAll<T>(string key)
  70. {
  71. return RedisHelper.HGetAll<T>(key);
  72. }
  73. #endregion
  74. #region 添加列表对象
  75. public long AddList(string key, object value)
  76. {
  77. return RedisHelper.LPush(key, value);
  78. // return 0;
  79. }
  80. public long AddList(string key, object[] value)
  81. {
  82. return RedisHelper.LPush(key, value);
  83. // return 0;
  84. }
  85. public long AddRightList(string key, object value)
  86. {
  87. return RedisHelper.RPush(key, value);
  88. // return 0;
  89. }
  90. public T RPop<T>(string key)
  91. {
  92. return RedisHelper.RPop<T>(key);
  93. }
  94. #endregion
  95. #region 添加集合对象
  96. public long SAdd(string key, object value)
  97. {
  98. return RedisHelper.SAdd(key, value);
  99. // return 0;
  100. }
  101. public long SAdd(string key, object[] value)
  102. {
  103. return RedisHelper.SAdd(key, value);
  104. // return 0;
  105. }
  106. #endregion
  107. #region 获取集合对象
  108. public T[] SGetList<T>(string key)
  109. {
  110. return RedisHelper.SMembers<T>(key);
  111. }
  112. #endregion
  113. #region 修改列表对象
  114. public bool SetList(string key, int index, object value)
  115. {
  116. long itemindex = RedisHelper.LLen(key) - index - 1;
  117. return RedisHelper.LSet(key, itemindex, value);
  118. // return false;
  119. }
  120. #endregion
  121. #region 获取列表
  122. public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
  123. {
  124. int start = (pageNum - 1) * pageSize;
  125. int end = start + pageSize - 1;
  126. string[] list = RedisHelper.LRange(key, start, end);
  127. List<T> lists = new List<T>();
  128. foreach (string record in list)
  129. {
  130. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  131. }
  132. return lists;
  133. }
  134. #endregion
  135. #region 移除列表对象
  136. public long DelFromList(string key, object value, int count = 1)
  137. {
  138. return RedisHelper.LRem(key, count, value);
  139. }
  140. #endregion
  141. #region 移动列表元素位置
  142. public T RPopLPush<T>(string key, string tokey)
  143. {
  144. return RedisHelper.RPopLPush<T>(key, tokey);
  145. }
  146. #endregion
  147. #region 添加排序列表对象
  148. public long AddSort(string key, object value, decimal score)
  149. {
  150. return RedisHelper.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 = RedisHelper.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 = RedisHelper.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 RedisHelper.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 RedisHelper.LLen(key);
  191. }
  192. public void Clear(string pattern)
  193. {
  194. string[] keys = RedisHelper.Keys(pattern);
  195. RedisHelper.Del(keys);
  196. }
  197. public string[] GetKeys(string pattern)
  198. {
  199. string[] keys = RedisHelper.Keys(pattern);
  200. return keys;
  201. }
  202. public void SetExpire(string key, int expire)
  203. {
  204. RedisHelper.Expire(key, expire); //秒为单位
  205. }
  206. }
  207. }