BothdisDbconn.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. namespace MySystem
  5. {
  6. public class BothdisDbconn
  7. {
  8. public readonly static BothdisDbconn Instance = new BothdisDbconn();
  9. #region 设置单个字段
  10. public void Set(string key, object value)
  11. {
  12. try
  13. {
  14. TendisDbconn.Instance.Set(key, value);
  15. }
  16. catch (Exception ex)
  17. {
  18. TendisErr err = new TendisErr()
  19. {
  20. key = key,
  21. value = value,
  22. errMsg = ex.ToString(),
  23. };
  24. RedisDbconn.Instance.AddList("Tendis:Set", err); // TODO:重新执行,最多重试3次
  25. }
  26. RedisDbconn.Instance.Set(key, value);
  27. }
  28. #endregion
  29. #region 整数累加
  30. public void AddInt(string key, long value = 1)
  31. {
  32. try
  33. {
  34. TendisDbconn.Instance.AddInt(key, value);
  35. }
  36. catch (Exception ex)
  37. {
  38. TendisErr err = new TendisErr()
  39. {
  40. key = key,
  41. value = value,
  42. errMsg = ex.ToString(),
  43. };
  44. RedisDbconn.Instance.AddList("Tendis:AddInt", err); // TODO:重新执行,最多重试3次
  45. }
  46. RedisDbconn.Instance.AddInt(key, value);
  47. }
  48. #endregion
  49. #region 数字累加
  50. public void AddNumber(string key, decimal value = 1)
  51. {
  52. try
  53. {
  54. TendisDbconn.Instance.AddNumber(key, value);
  55. }
  56. catch (Exception ex)
  57. {
  58. TendisErr err = new TendisErr()
  59. {
  60. key = key,
  61. value = value,
  62. errMsg = ex.ToString(),
  63. };
  64. RedisDbconn.Instance.AddList("Tendis:AddNumber", err); // TODO:重新执行,最多重试3次
  65. }
  66. RedisDbconn.Instance.AddNumber(key, value);
  67. }
  68. #endregion
  69. #region 获取单个字段
  70. public T Get<T>(string key)
  71. {
  72. T obj = RedisDbconn.Instance.Get<T>(key);
  73. if (obj != null)
  74. {
  75. return obj;
  76. }
  77. T newobj = TendisDbconn.Instance.Get<T>(key);
  78. if (newobj != null)
  79. {
  80. RedisDbconn.Instance.Set(key, newobj);
  81. }
  82. return newobj;
  83. }
  84. #endregion
  85. #region 设置散列字段
  86. public void HSet(string key, string field, object value)
  87. {
  88. try
  89. {
  90. TendisDbconn.Instance.HSet(key, field, value);
  91. }
  92. catch (Exception ex)
  93. {
  94. TendisErr err = new TendisErr()
  95. {
  96. key = key,
  97. value = value,
  98. errMsg = ex.ToString(),
  99. };
  100. RedisDbconn.Instance.AddList("Tendis:HSet", err); // TODO:重新执行,最多重试3次
  101. }
  102. RedisDbconn.Instance.HSet(key, field, value);
  103. }
  104. #endregion
  105. #region 散列整数累加
  106. public void HAddInt(string key, string field, long value = 1)
  107. {
  108. try
  109. {
  110. TendisDbconn.Instance.HAddInt(key, field, value);
  111. }
  112. catch (Exception ex)
  113. {
  114. TendisErr err = new TendisErr()
  115. {
  116. key = key,
  117. value = value,
  118. errMsg = ex.ToString(),
  119. };
  120. RedisDbconn.Instance.AddList("Tendis:HAddInt", err); // TODO:重新执行,最多重试3次
  121. }
  122. RedisDbconn.Instance.HAddInt(key, field, value);
  123. }
  124. #endregion
  125. #region 散列数字累加
  126. public void HAddNumber(string key, string field, decimal value = 1)
  127. {
  128. try
  129. {
  130. TendisDbconn.Instance.HAddNumber(key, field, value);
  131. }
  132. catch (Exception ex)
  133. {
  134. TendisErr err = new TendisErr()
  135. {
  136. key = key,
  137. value = value,
  138. errMsg = ex.ToString(),
  139. };
  140. RedisDbconn.Instance.AddList("Tendis:HAddNumber", err); // TODO:重新执行,最多重试3次
  141. }
  142. RedisDbconn.Instance.HAddNumber(key, field, value);
  143. }
  144. #endregion
  145. #region 获取散列元素
  146. public T HGet<T>(string key, string field)
  147. {
  148. T obj = RedisDbconn.Instance.HGet<T>(key, field);
  149. if (obj != null)
  150. {
  151. return obj;
  152. }
  153. T newobj = TendisDbconn.Instance.HGet<T>(key, field);
  154. if (newobj != null)
  155. {
  156. RedisDbconn.Instance.HSet(key, field, newobj);
  157. }
  158. return newobj;
  159. }
  160. #endregion
  161. #region 获取散列所有元素
  162. public Dictionary<string, T> HGetAll<T>(string key)
  163. {
  164. Dictionary<string, T> obj = RedisDbconn.Instance.HGetAll<T>(key);
  165. if (obj != null)
  166. {
  167. return obj;
  168. }
  169. Dictionary<string, T> newobj = TendisDbconn.Instance.HGetAll<T>(key);
  170. if (newobj != null)
  171. {
  172. foreach (string sub in newobj.Keys)
  173. {
  174. RedisDbconn.Instance.HSet(key, sub, newobj[sub]);
  175. }
  176. }
  177. return newobj;
  178. }
  179. #endregion
  180. #region 添加集合对象
  181. public void SAdd(string key, object value)
  182. {
  183. try
  184. {
  185. TendisDbconn.Instance.SAdd(key, value);
  186. }
  187. catch (Exception ex)
  188. {
  189. TendisErr err = new TendisErr()
  190. {
  191. key = key,
  192. value = value,
  193. errMsg = ex.ToString(),
  194. };
  195. RedisDbconn.Instance.AddList("Tendis:SAdd", err); // TODO:重新执行,最多重试3次
  196. }
  197. RedisDbconn.Instance.SAdd(key, value);
  198. }
  199. public void SAdd(string key, object[] value)
  200. {
  201. try
  202. {
  203. TendisDbconn.Instance.SAdd(key, value);
  204. }
  205. catch (Exception ex)
  206. {
  207. TendisErr err = new TendisErr()
  208. {
  209. key = key,
  210. value = value,
  211. errMsg = ex.ToString(),
  212. };
  213. RedisDbconn.Instance.AddList("Tendis:SAdd", err); // TODO:重新执行,最多重试3次
  214. }
  215. RedisDbconn.Instance.SAdd(key, value);
  216. }
  217. #endregion
  218. #region 获取集合对象
  219. public T[] SGetList<T>(string key)
  220. {
  221. T[] obj = RedisDbconn.Instance.SGetList<T>(key);
  222. if (obj != null)
  223. {
  224. if (obj.Length > 0)
  225. {
  226. return obj;
  227. }
  228. }
  229. T[] newobj = TendisDbconn.Instance.SGetList<T>(key);
  230. if (newobj != null)
  231. {
  232. foreach (T sub in newobj)
  233. {
  234. RedisDbconn.Instance.SAdd(key, sub);
  235. }
  236. }
  237. return newobj;
  238. }
  239. #endregion
  240. #region 添加列表对象
  241. public void AddList(string key, object value)
  242. {
  243. try
  244. {
  245. TendisDbconn.Instance.AddList(key, value);
  246. }
  247. catch (Exception ex)
  248. {
  249. TendisErr err = new TendisErr()
  250. {
  251. key = key,
  252. value = value,
  253. errMsg = ex.ToString(),
  254. };
  255. RedisDbconn.Instance.AddList("Tendis:AddList", err); // TODO:重新执行,最多重试3次
  256. }
  257. RedisDbconn.Instance.AddList(key, value);
  258. int Expired = 60 * 60 * 24 * 180;
  259. RedisDbconn.Instance.SetExpire(key, Expired);
  260. }
  261. public void AddList(string key, object[] value)
  262. {
  263. try
  264. {
  265. TendisDbconn.Instance.AddList(key, value);
  266. }
  267. catch (Exception ex)
  268. {
  269. TendisErr err = new TendisErr()
  270. {
  271. key = key,
  272. value = value,
  273. errMsg = ex.ToString(),
  274. };
  275. RedisDbconn.Instance.AddList("Tendis:AddList", err); // TODO:重新执行,最多重试3次
  276. }
  277. RedisDbconn.Instance.AddList(key, value);
  278. int Expired = 60 * 60 * 24 * 180;
  279. RedisDbconn.Instance.SetExpire(key, Expired);
  280. }
  281. #endregion
  282. #region 获取列表
  283. public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
  284. {
  285. List<T> list = RedisDbconn.Instance.GetList<T>(key, pageNum, pageSize);
  286. if (list.Count > 0)
  287. {
  288. return list;
  289. }
  290. return TendisDbconn.Instance.GetList<T>(key, pageNum, pageSize);
  291. }
  292. #endregion
  293. #region 添加排序列表对象
  294. public void AddSort(string key, object value, decimal score)
  295. {
  296. try
  297. {
  298. TendisDbconn.Instance.AddSort(key, value, score);
  299. }
  300. catch (Exception ex)
  301. {
  302. TendisErr err = new TendisErr()
  303. {
  304. key = key,
  305. value = value,
  306. errMsg = ex.ToString(),
  307. };
  308. RedisDbconn.Instance.AddList("Tendis:AddSort", err); // TODO:重新执行,最多重试3次
  309. }
  310. RedisDbconn.Instance.AddSort(key, value, score);
  311. int Expired = 60 * 60 * 24 * 180;
  312. RedisDbconn.Instance.SetExpire(key, Expired);
  313. }
  314. #endregion
  315. #region 获取排序列表
  316. public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
  317. {
  318. List<T> list = RedisDbconn.Instance.GetSort<T>(key, pageNum, pageSize);
  319. if (list.Count > 0)
  320. {
  321. return list;
  322. }
  323. return TendisDbconn.Instance.GetSort<T>(key, pageNum, pageSize);
  324. }
  325. public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
  326. {
  327. List<T> list = RedisDbconn.Instance.GetSortDesc<T>(key, pageNum, pageSize);
  328. if (list.Count > 0)
  329. {
  330. return list;
  331. }
  332. return TendisDbconn.Instance.GetSortDesc<T>(key, pageNum, pageSize);
  333. }
  334. #endregion
  335. public void Remove(string key, long start, long end)
  336. {
  337. try
  338. {
  339. TendisDbconn.Instance.AddSort(key, start, end);
  340. }
  341. catch (Exception ex)
  342. {
  343. TendisErr err = new TendisErr()
  344. {
  345. key = key,
  346. start = start,
  347. end = end,
  348. errMsg = ex.ToString(),
  349. };
  350. RedisDbconn.Instance.AddList("Tendis:Remove", err); // TODO:重新执行,最多重试3次
  351. }
  352. RedisDbconn.Instance.Remove(key, start, end);
  353. }
  354. public void RemoveTop(string key, long count)
  355. {
  356. Remove(key, count, RedisDbconn.Instance.Count(key) - 1); ;
  357. }
  358. public void Clear(string pattern)
  359. {
  360. try
  361. {
  362. TendisDbconn.Instance.Clear(pattern);
  363. }
  364. catch (Exception ex)
  365. {
  366. TendisErr err = new TendisErr()
  367. {
  368. key = pattern,
  369. errMsg = ex.ToString(),
  370. };
  371. RedisDbconn.Instance.AddList("Tendis:Clear", err); // TODO:重新执行,最多重试3次
  372. }
  373. RedisDbconn.Instance.Clear(pattern);
  374. }
  375. }
  376. }