BothdisDbconn.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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(RedisDbconn.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. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Set"); // TODO:重新执行,最多重试3次
  37. }
  38. }
  39. #endregion
  40. #region 整数累加
  41. public void AddInt(string key, long value = 1)
  42. {
  43. try
  44. {
  45. if (RedisDbconn.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. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddInt"); // TODO:重新执行,最多重试3次
  59. }
  60. }
  61. #endregion
  62. #region 数字累加
  63. public void AddNumber(string key, decimal value = 1)
  64. {
  65. try
  66. {
  67. if (RedisDbconn.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. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddNumber"); // TODO:重新执行,最多重试3次
  81. }
  82. }
  83. #endregion
  84. #region 获取单个字段
  85. public T Get<T>(string key)
  86. {
  87. if (RedisDbconn.Instance.Exists(key))
  88. {
  89. T obj = RedisDbconn.Instance.Get<T>(key);
  90. if (obj != null)
  91. {
  92. return obj;
  93. }
  94. }
  95. T newobj = RedisDbconn.Instance.Get<T>(key);
  96. if (newobj != null)
  97. {
  98. RedisDbconn.Instance.Set(key, newobj);
  99. }
  100. return newobj;
  101. }
  102. #endregion
  103. #region 设置散列字段
  104. public void HSet(string key, string field, object value)
  105. {
  106. try
  107. {
  108. RedisDbconn.Instance.HSet(key, field, value);
  109. }
  110. catch (Exception ex)
  111. {
  112. TendisErr err = new TendisErr()
  113. {
  114. key = key,
  115. value = value,
  116. errMsg = ex.ToString(),
  117. };
  118. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HSet"); // TODO:重新执行,最多重试3次
  119. }
  120. RedisDbconn.Instance.HSet(key, field, value);
  121. }
  122. #endregion
  123. #region 散列整数累加
  124. public void HAddInt(string key, string field, long value = 1)
  125. {
  126. try
  127. {
  128. RedisDbconn.Instance.HAddInt(key, field, value);
  129. }
  130. catch (Exception ex)
  131. {
  132. TendisErr err = new TendisErr()
  133. {
  134. key = key,
  135. value = value,
  136. errMsg = ex.ToString(),
  137. };
  138. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HAddInt"); // TODO:重新执行,最多重试3次
  139. }
  140. RedisDbconn.Instance.HAddInt(key, field, value);
  141. }
  142. #endregion
  143. #region 散列数字累加
  144. public void HAddNumber(string key, string field, decimal value = 1)
  145. {
  146. try
  147. {
  148. RedisDbconn.Instance.HAddNumber(key, field, value);
  149. }
  150. catch (Exception ex)
  151. {
  152. TendisErr err = new TendisErr()
  153. {
  154. key = key,
  155. value = value,
  156. errMsg = ex.ToString(),
  157. };
  158. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:HAddNumber"); // TODO:重新执行,最多重试3次
  159. }
  160. RedisDbconn.Instance.HAddNumber(key, field, value);
  161. }
  162. #endregion
  163. #region 获取散列元素
  164. public T HGet<T>(string key, string field)
  165. {
  166. if (RedisDbconn.Instance.HExists(key, field))
  167. {
  168. T obj = RedisDbconn.Instance.HGet<T>(key, field);
  169. if (obj != null)
  170. {
  171. return obj;
  172. }
  173. }
  174. T newobj = RedisDbconn.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. if (RedisDbconn.Instance.Exists(key))
  186. {
  187. Dictionary<string, T> obj = RedisDbconn.Instance.HGetAll<T>(key);
  188. if (obj != null)
  189. {
  190. return obj;
  191. }
  192. }
  193. Dictionary<string, T> newobj = RedisDbconn.Instance.HGetAll<T>(key);
  194. // if (newobj != null)
  195. // {
  196. // foreach (string sub in newobj.Keys)
  197. // {
  198. // RedisDbconn.Instance.HSet(key, sub, newobj[sub]);
  199. // }
  200. // }
  201. return newobj;
  202. }
  203. #endregion
  204. #region 添加集合对象
  205. public void SAdd(string key, object value)
  206. {
  207. try
  208. {
  209. RedisDbconn.Instance.SAdd(key, value);
  210. }
  211. catch (Exception ex)
  212. {
  213. TendisErr err = new TendisErr()
  214. {
  215. key = key,
  216. value = value,
  217. errMsg = ex.ToString(),
  218. };
  219. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:SAdd"); // TODO:重新执行,最多重试3次
  220. }
  221. RedisDbconn.Instance.SAdd(key, value);
  222. }
  223. public void SAdd(string key, object[] value)
  224. {
  225. try
  226. {
  227. RedisDbconn.Instance.SAdd(key, value);
  228. }
  229. catch (Exception ex)
  230. {
  231. TendisErr err = new TendisErr()
  232. {
  233. key = key,
  234. value = value,
  235. errMsg = ex.ToString(),
  236. };
  237. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:SAdd"); // TODO:重新执行,最多重试3次
  238. }
  239. RedisDbconn.Instance.SAdd(key, value);
  240. }
  241. #endregion
  242. #region 获取集合对象
  243. public T[] SGetList<T>(string key)
  244. {
  245. if (RedisDbconn.Instance.Exists(key))
  246. {
  247. T[] obj = RedisDbconn.Instance.SGetList<T>(key);
  248. if (obj != null)
  249. {
  250. if (obj.Length > 0)
  251. {
  252. return obj;
  253. }
  254. }
  255. }
  256. T[] newobj = RedisDbconn.Instance.SGetList<T>(key);
  257. // if (newobj != null)
  258. // {
  259. // foreach (T sub in newobj)
  260. // {
  261. // RedisDbconn.Instance.SAdd(key, sub);
  262. // }
  263. // }
  264. return newobj;
  265. }
  266. #endregion
  267. #region 添加列表对象
  268. public void AddList(string key, object value)
  269. {
  270. try
  271. {
  272. if (RedisDbconn.Instance.AddList(key, value) > 0)
  273. {
  274. RedisDbconn.Instance.Clear(key);
  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. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddList"); // TODO:重新执行,最多重试3次
  286. }
  287. }
  288. public void AddList(string key, object[] value)
  289. {
  290. try
  291. {
  292. RedisDbconn.Instance.AddList(key, value);
  293. }
  294. catch (Exception ex)
  295. {
  296. TendisErr err = new TendisErr()
  297. {
  298. key = key,
  299. value = value,
  300. errMsg = ex.ToString(),
  301. };
  302. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddList"); // TODO:重新执行,最多重试3次
  303. }
  304. RedisDbconn.Instance.AddList(key, value);
  305. int Expired = 60 * 60 * 24 * 180;
  306. RedisDbconn.Instance.SetExpire(key, Expired);
  307. }
  308. #endregion
  309. #region 获取列表
  310. public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
  311. {
  312. if (RedisDbconn.Instance.Exists(key))
  313. {
  314. List<T> list = RedisDbconn.Instance.GetList<T>(key, pageNum, pageSize);
  315. if (list.Count > 0)
  316. {
  317. return list;
  318. }
  319. }
  320. List<T> tlist = RedisDbconn.Instance.GetList<T>(key, pageNum, pageSize);
  321. if (tlist.Count > 0)
  322. {
  323. RedisDbconn.Instance.Clear(key);
  324. object[] tmplist = new object[tlist.Count];
  325. for (int i = 0; i < tlist.Count; i++)
  326. {
  327. tmplist[i] = tlist[i];
  328. }
  329. RedisDbconn.Instance.AddList(key, tmplist);
  330. }
  331. return tlist;
  332. }
  333. #endregion
  334. #region 添加排序列表对象
  335. public void AddSort(string key, object value, decimal score)
  336. {
  337. try
  338. {
  339. RedisDbconn.Instance.AddSort(key, value, score);
  340. }
  341. catch (Exception ex)
  342. {
  343. TendisErr err = new TendisErr()
  344. {
  345. key = key,
  346. value = value,
  347. errMsg = ex.ToString(),
  348. };
  349. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:AddSort"); // TODO:重新执行,最多重试3次
  350. }
  351. RedisDbconn.Instance.AddSort(key, value, score);
  352. int Expired = 60 * 60 * 24 * 180;
  353. RedisDbconn.Instance.SetExpire(key, Expired);
  354. }
  355. #endregion
  356. #region 获取排序列表
  357. public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
  358. {
  359. if (RedisDbconn.Instance.Exists(key))
  360. {
  361. List<T> list = RedisDbconn.Instance.GetSort<T>(key, pageNum, pageSize);
  362. if (list.Count > 0)
  363. {
  364. return list;
  365. }
  366. }
  367. return RedisDbconn.Instance.GetSort<T>(key, pageNum, pageSize);
  368. }
  369. public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
  370. {
  371. if (RedisDbconn.Instance.Exists(key))
  372. {
  373. List<T> list = RedisDbconn.Instance.GetSortDesc<T>(key, pageNum, pageSize);
  374. if (list.Count > 0)
  375. {
  376. return list;
  377. }
  378. }
  379. return RedisDbconn.Instance.GetSortDesc<T>(key, pageNum, pageSize);
  380. }
  381. #endregion
  382. public void Remove(string key, long start, long end)
  383. {
  384. try
  385. {
  386. RedisDbconn.Instance.AddSort(key, start, end);
  387. }
  388. catch (Exception ex)
  389. {
  390. TendisErr err = new TendisErr()
  391. {
  392. key = key,
  393. start = start,
  394. end = end,
  395. errMsg = ex.ToString(),
  396. };
  397. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Remove"); // TODO:重新执行,最多重试3次
  398. }
  399. RedisDbconn.Instance.Remove(key, start, end);
  400. }
  401. public void RemoveTop(string key, long count)
  402. {
  403. Remove(key, count, RedisDbconn.Instance.Count(key) - 1); ;
  404. }
  405. public void Clear(string pattern)
  406. {
  407. try
  408. {
  409. RedisDbconn.Instance.Clear(pattern);
  410. RedisDbconn.Instance.Clear(pattern);
  411. }
  412. catch (Exception ex)
  413. {
  414. TendisErr err = new TendisErr()
  415. {
  416. key = pattern,
  417. errMsg = ex.ToString(),
  418. };
  419. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(err), "Tendis:Clear"); // TODO:重新执行,最多重试3次
  420. }
  421. }
  422. }
  423. }