RedisDbconn.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. return RedisHelper.LSet(key, index, 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. string[] list = RedisHelper.LRange(key, start, end);
  126. List<T> lists = new List<T>();
  127. foreach (string record in list)
  128. {
  129. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  130. }
  131. return lists;
  132. }
  133. #endregion
  134. #region 移除列表对象
  135. public long DelFromList(string key, object value, int count = 1)
  136. {
  137. return RedisHelper.LRem(key, count, value);
  138. }
  139. #endregion
  140. #region 移动列表元素位置
  141. public T RPopLPush<T>(string key, string tokey)
  142. {
  143. return RedisHelper.RPopLPush<T>(key, tokey);
  144. }
  145. #endregion
  146. #region 添加排序列表对象
  147. public long AddSort(string key, object value, decimal score)
  148. {
  149. return RedisHelper.ZAdd(key, (score, value));
  150. // return 0;
  151. }
  152. #endregion
  153. #region 获取排序列表
  154. public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
  155. {
  156. int start = (pageNum - 1) * pageSize;
  157. int end = start + pageSize;
  158. string[] list = RedisHelper.ZRangeByScore(key, start, end);
  159. List<T> lists = new List<T>();
  160. foreach (string record in list)
  161. {
  162. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  163. }
  164. return lists;
  165. }
  166. public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
  167. {
  168. int start = (pageNum - 1) * pageSize;
  169. int end = start + pageSize;
  170. string[] list = RedisHelper.ZRevRangeByScore(key, start, end);
  171. List<T> lists = new List<T>();
  172. foreach (string record in list)
  173. {
  174. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  175. }
  176. return lists;
  177. }
  178. #endregion
  179. public bool Remove(string key, long start, long end)
  180. {
  181. return RedisHelper.LTrim(key, start, end);
  182. }
  183. public bool RemoveTop(string key, long count)
  184. {
  185. return RedisDbconn.Instance.Remove(key, count, RedisDbconn.Instance.Count(key) - 1);;
  186. }
  187. public long Count(string key)
  188. {
  189. return RedisHelper.LLen(key);
  190. }
  191. public void Clear(string pattern)
  192. {
  193. string[] keys = RedisHelper.Keys(pattern);
  194. RedisHelper.Del(keys);
  195. }
  196. public string[] GetKeys(string pattern)
  197. {
  198. string[] keys = RedisHelper.Keys(pattern);
  199. return keys;
  200. }
  201. public void SetExpire(string key, int expire)
  202. {
  203. RedisHelper.Expire(key, expire); //秒为单位
  204. }
  205. }
  206. }