RedisUtil.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using CoreEntity.Entity;
  2. using Newtonsoft.Json;
  3. using NPOI.SS.Formula.Functions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.Serialization.Json;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace MemberWeb.Commonss
  13. {
  14. public static class RedisUtil
  15. {
  16. public static string connectionredis = "192.168.50.41:6379,poolsize=3,tryit=0,password=360lj.com!";
  17. //public static string connectionredis = "127.0.0.1:6379,poolsize=3,tryit=0";
  18. public static int timeCont = 86400;//数据过期时间
  19. public static DateTime dateTime = DateTime.Parse(DateTime.Now.ToString());
  20. /// <summary>
  21. /// 保存数据库缓存
  22. /// </summary>
  23. /// <param name="objList"></param>
  24. /// <param name="key"></param>
  25. public static void addRedisUtils(IList<Mem_UserDrug> objList, string key) {
  26. //连接
  27. var csredis = new CSRedis.CSRedisClient(connectionredis);
  28. //var csredis = new CSRedis.CSRedisClient("redis-master", new[] { "192.168.50.34:6379" });
  29. key = dateTime.ToString("yyyy-MM-dd") + key;
  30. //初始化 RedisHelper
  31. RedisHelper.Initialization(csredis);
  32. try
  33. {
  34. foreach (Object dr in objList)
  35. {
  36. csredis.RPush(key, dr);
  37. }
  38. //csredis.Expire(key, timeCont);
  39. }
  40. catch (Exception ex)
  41. {
  42. Console.WriteLine(ex.ToString());
  43. }
  44. }
  45. /// <summary>
  46. /// 保存es数据缓存
  47. /// </summary>
  48. /// <param name="esObjstr"></param>
  49. /// <param name="key"></param>
  50. public static void addRedisEsUtils(string esObjstr, string key)
  51. {
  52. //连接
  53. var csredis = new CSRedis.CSRedisClient(connectionredis);
  54. key = dateTime.ToString("yyyy-MM-dd") + key;
  55. //初始化 RedisHelper
  56. RedisHelper.Initialization(csredis);
  57. try
  58. {
  59. csredis.RPush(key, esObjstr);
  60. //csredis.Expire(key, timeCont);
  61. }
  62. catch (Exception ex)
  63. {
  64. Console.WriteLine(ex.ToString());
  65. }
  66. }
  67. /// <summary>
  68. /// 获取数据库缓存
  69. /// </summary>
  70. /// <param name="key"></param>
  71. /// <returns></returns>
  72. public static IList<Mem_UserDrugv2> getRedisUtils(string key)
  73. {
  74. IList<Mem_UserDrugv2> permss = new List<Mem_UserDrugv2>();
  75. //连接
  76. var csredis = new CSRedis.CSRedisClient(connectionredis);
  77. key = dateTime.ToString("yyyy-MM-dd") + key;
  78. //初始化 RedisHelper
  79. RedisHelper.Initialization(csredis);
  80. long count=RedisHelper.LLen(key);
  81. if (count>0) {
  82. foreach (var item in csredis.LRange(key, 0, -1))
  83. {
  84. Mem_UserDrugv2 men = new Mem_UserDrugv2();
  85. System.Diagnostics.Debug.WriteLine(item);
  86. Mem_UserDrugv2 stobj3 = (Mem_UserDrugv2)JsonToObject(item, men);
  87. permss.Add(stobj3);
  88. }
  89. }
  90. return permss;
  91. }
  92. /// <summary>
  93. /// 获取es缓存
  94. /// </summary>
  95. /// <param name="key"></param>
  96. /// <returns></returns>
  97. public static string getRedisEsUtils(string key)
  98. {
  99. //连接
  100. var csredis = new CSRedis.CSRedisClient(connectionredis);
  101. key = dateTime.ToString("yyyy-MM-dd") + key;
  102. //初始化 RedisHelper
  103. string esObjstr = "";
  104. RedisHelper.Initialization(csredis);
  105. long count = RedisHelper.LLen(key);
  106. if (count > 0)
  107. {
  108. foreach (var item in csredis.LRange(key, 0, -1))
  109. {
  110. esObjstr = item;
  111. System.Diagnostics.Debug.WriteLine(item);
  112. }
  113. }
  114. return esObjstr;
  115. }
  116. public static object JsonToObject(string jsonString, object obj)
  117. {
  118. DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
  119. MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
  120. return serializer.ReadObject(mStream);
  121. }
  122. }
  123. }