TendisDbconn.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System.Collections.Generic;
  2. using Library;
  3. using System.Linq;
  4. namespace MySystem
  5. {
  6. public class TendisDbconn
  7. {
  8. public readonly static TendisDbconn Instance = new TendisDbconn();
  9. public static CSRedis.CSRedisClient csredis;
  10. private TendisDbconn()
  11. {
  12. if (csredis == null)
  13. {
  14. csredis = new CSRedis.CSRedisClient(ConfigurationManager.AppSettings["TendisConnStr"].ToString());
  15. }
  16. }
  17. #region 设置单个字段
  18. public bool Set(string key, object value)
  19. {
  20. return csredis.Set(key, value);
  21. // return false;
  22. }
  23. #endregion
  24. #region 整数累加
  25. public long AddInt(string key, long value = 1)
  26. {
  27. return csredis.IncrBy(key, value);
  28. // return 0;
  29. }
  30. #endregion
  31. #region 数字累加
  32. public decimal AddNumber(string key, decimal value = 1)
  33. {
  34. return csredis.IncrByFloat(key, value);
  35. // return 0;
  36. }
  37. #endregion
  38. #region 获取单个字段
  39. public T Get<T>(string key)
  40. {
  41. return csredis.Get<T>(key);
  42. }
  43. #endregion
  44. #region 设置散列字段
  45. public bool HSet(string key, string field, object value)
  46. {
  47. return csredis.HSet(key, field, value);
  48. // return false;
  49. }
  50. #endregion
  51. #region 散列整数累加
  52. public long HAddInt(string key, string field, long value = 1)
  53. {
  54. return csredis.HIncrBy(key, field, value);
  55. // return 0;
  56. }
  57. #endregion
  58. #region 散列数字累加
  59. public decimal HAddNumber(string key, string field, decimal value = 1)
  60. {
  61. return csredis.HIncrByFloat(key, field, value);
  62. // return 0;
  63. }
  64. #endregion
  65. #region 获取散列元素
  66. public T HGet<T>(string key, string field)
  67. {
  68. return csredis.HGet<T>(key, field);
  69. }
  70. #endregion
  71. #region 获取散列所有元素
  72. public Dictionary<string, T> HGetAll<T>(string key)
  73. {
  74. return csredis.HGetAll<T>(key);
  75. }
  76. #endregion
  77. #region 添加列表对象
  78. public long AddList(string key, object value)
  79. {
  80. return csredis.LPush(key, value);
  81. // return 0;
  82. }
  83. public long AddList(string key, object[] value)
  84. {
  85. return csredis.LPush(key, value);
  86. // return 0;
  87. }
  88. public T RPop<T>(string key)
  89. {
  90. return csredis.RPop<T>(key);
  91. }
  92. #endregion
  93. #region 添加集合对象
  94. public long SAdd(string key, object value)
  95. {
  96. return csredis.SAdd(key, value);
  97. // return 0;
  98. }
  99. public long SAdd(string key, object[] value)
  100. {
  101. return csredis.SAdd(key, value);
  102. // return 0;
  103. }
  104. #endregion
  105. #region 判断元素是否存在
  106. public bool SIsMember(string key, object value)
  107. {
  108. return csredis.SIsMember(key, value);
  109. }
  110. #endregion
  111. #region 获取集合对象
  112. public T[] SGetList<T>(string key)
  113. {
  114. return csredis.SMembers<T>(key);
  115. }
  116. #endregion
  117. #region 修改列表对象
  118. public bool SetList(string key, int index, object value)
  119. {
  120. long itemindex = csredis.LLen(key) - index - 1;
  121. return csredis.LSet(key, itemindex, value);
  122. // return false;
  123. }
  124. #endregion
  125. #region 获取列表
  126. public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
  127. {
  128. int start = (pageNum - 1) * pageSize;
  129. int end = start + pageSize - 1;
  130. T[] list = csredis.LRange<T>(key, start, end);
  131. return list.ToList();
  132. }
  133. #endregion
  134. #region 添加排序列表对象
  135. public long AddSort(string key, object value, decimal score)
  136. {
  137. return csredis.ZAdd(key, (score, value));
  138. // return 0;
  139. }
  140. #endregion
  141. #region 获取排序列表
  142. public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
  143. {
  144. int start = (pageNum - 1) * pageSize;
  145. int end = start + pageSize;
  146. string[] list = csredis.ZRangeByScore(key, start, end);
  147. List<T> lists = new List<T>();
  148. foreach (string record in list)
  149. {
  150. lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
  151. }
  152. return lists;
  153. }
  154. public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
  155. {
  156. int start = (pageNum - 1) * pageSize;
  157. int end = start + pageSize;
  158. string[] list = csredis.ZRevRangeByScore(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. #endregion
  167. public bool Remove(string key, long start, long end)
  168. {
  169. return csredis.LTrim(key, start, end);
  170. }
  171. public bool RemoveTop(string key, long count)
  172. {
  173. return RedisDbconn.Instance.Remove(key, count, RedisDbconn.Instance.Count(key) - 1);;
  174. }
  175. public long Count(string key)
  176. {
  177. return csredis.LLen(key);
  178. }
  179. public void Clear(string pattern)
  180. {
  181. string[] keys = csredis.Keys(pattern);
  182. csredis.Del(keys);
  183. }
  184. public string[] GetKeys(string pattern)
  185. {
  186. string[] keys = csredis.Keys(pattern);
  187. return keys;
  188. }
  189. public void SetExpire(string key, int expire)
  190. {
  191. csredis.Expire(key, expire); //秒为单位
  192. }
  193. }
  194. }