using CoreEntity.Entity; using Newtonsoft.Json; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; namespace MemberWeb.Commonss { public static class RedisUtil { public static string connectionredis = "192.168.50.41:6379,poolsize=3,tryit=0,password=360lj.com!"; //public static string connectionredis = "127.0.0.1:6379,poolsize=3,tryit=0"; public static int timeCont = 86400;//数据过期时间 public static DateTime dateTime = DateTime.Parse(DateTime.Now.ToString()); /// /// 保存数据库缓存 /// /// /// public static void addRedisUtils(IList objList, string key) { //连接 var csredis = new CSRedis.CSRedisClient(connectionredis); //var csredis = new CSRedis.CSRedisClient("redis-master", new[] { "192.168.50.34:6379" }); key = dateTime.ToString("yyyy-MM-dd") + key; //初始化 RedisHelper RedisHelper.Initialization(csredis); try { foreach (Object dr in objList) { csredis.RPush(key, dr); } //csredis.Expire(key, timeCont); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } /// /// 保存es数据缓存 /// /// /// public static void addRedisEsUtils(string esObjstr, string key) { //连接 var csredis = new CSRedis.CSRedisClient(connectionredis); key = dateTime.ToString("yyyy-MM-dd") + key; //初始化 RedisHelper RedisHelper.Initialization(csredis); try { csredis.RPush(key, esObjstr); //csredis.Expire(key, timeCont); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } /// /// 获取数据库缓存 /// /// /// public static IList getRedisUtils(string key) { IList permss = new List(); //连接 var csredis = new CSRedis.CSRedisClient(connectionredis); key = dateTime.ToString("yyyy-MM-dd") + key; //初始化 RedisHelper RedisHelper.Initialization(csredis); long count=RedisHelper.LLen(key); if (count>0) { foreach (var item in csredis.LRange(key, 0, -1)) { Mem_UserDrugv2 men = new Mem_UserDrugv2(); System.Diagnostics.Debug.WriteLine(item); Mem_UserDrugv2 stobj3 = (Mem_UserDrugv2)JsonToObject(item, men); permss.Add(stobj3); } } return permss; } /// /// 获取es缓存 /// /// /// public static string getRedisEsUtils(string key) { //连接 var csredis = new CSRedis.CSRedisClient(connectionredis); key = dateTime.ToString("yyyy-MM-dd") + key; //初始化 RedisHelper string esObjstr = ""; RedisHelper.Initialization(csredis); long count = RedisHelper.LLen(key); if (count > 0) { foreach (var item in csredis.LRange(key, 0, -1)) { esObjstr = item; System.Diagnostics.Debug.WriteLine(item); } } return esObjstr; } public static object JsonToObject(string jsonString, object obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); return serializer.ReadObject(mStream); } } }