BothdisDbconn.cs 15 KB

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