123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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());
- /// <summary>
- /// 保存数据库缓存
- /// </summary>
- /// <param name="objList"></param>
- /// <param name="key"></param>
- public static void addRedisUtils(IList<Mem_UserDrug> 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());
- }
- }
- /// <summary>
- /// 保存es数据缓存
- /// </summary>
- /// <param name="esObjstr"></param>
- /// <param name="key"></param>
- 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());
- }
- }
- /// <summary>
- /// 获取数据库缓存
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static IList<Mem_UserDrugv2> getRedisUtils(string key)
- {
- IList<Mem_UserDrugv2> permss = new List<Mem_UserDrugv2>();
- //连接
- 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;
- }
- /// <summary>
- /// 获取es缓存
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
|