RedisDbconn.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 long AddSort(string key, object value, decimal score)
  143. {
  144. return RedisHelper.ZAdd(key, (score, value));
  145. // return 0;
  146. }
  147. #endregion
  148. #region 获取排序列表
  149. public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
  150. {
  151. int start = (pageNum - 1) * pageSize;
  152. int end = start + pageSize;
  153. string[] list = RedisHelper.ZRangeByScore(key, start, end);
  154. List<T> lists = new List<T>();
  155. foreach (string record in list)
  156. {
  157. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  158. }
  159. return lists;
  160. }
  161. public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
  162. {
  163. int start = (pageNum - 1) * pageSize;
  164. int end = start + pageSize;
  165. string[] list = RedisHelper.ZRevRangeByScore(key, start, end);
  166. List<T> lists = new List<T>();
  167. foreach (string record in list)
  168. {
  169. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  170. }
  171. return lists;
  172. }
  173. #endregion
  174. public bool Remove(string key, long start, long end)
  175. {
  176. return RedisHelper.LTrim(key, start, end);
  177. }
  178. public bool RemoveTop(string key, long count)
  179. {
  180. return RedisDbconn.Instance.Remove(key, count, RedisDbconn.Instance.Count(key) - 1);;
  181. }
  182. public long Count(string key)
  183. {
  184. return RedisHelper.LLen(key);
  185. }
  186. public void Clear(string pattern)
  187. {
  188. string[] keys = RedisHelper.Keys(pattern);
  189. RedisHelper.Del(keys);
  190. }
  191. public string[] GetKeys(string pattern)
  192. {
  193. string[] keys = RedisHelper.Keys(pattern);
  194. return keys;
  195. }
  196. public void SetExpire(string key, int expire)
  197. {
  198. RedisHelper.Expire(key, expire); //秒为单位
  199. }
  200. }
  201. }