BothdisDbconn.cs 14 KB

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