RedisDbconn.cs 6.3 KB

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