using CommonTools; using CSRedis; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; namespace Common { /// /// Redis缓存辅助类 /// public class CSRedisCacheHelper { public static CSRedisClient? redis; public static CSRedisClient? redis0; public static CSRedisClient? redis1; public static CSRedisClient? redis2; public static CSRedisClient? redis3; public static CSRedisClient? redis4; public static CSRedisClient? redis5; private const string ip = "127.0.0.1"; private const string port = "6379"; static CSRedisCacheHelper() { var redisHostStr = string.Format("{0}:{1}", ip, port); if (!string.IsNullOrEmpty(redisHostStr)) { redis0 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=0"); redis = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1"); redis2 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=2"); redis3 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=3"); redis4 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=4"); //准备存储取电数据 redis5 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=5"); //Native subscribe string channel = "__keyevent@4__:expired"; var QQQ = new ValueTuple>(channel, (msg) => { //string Key = CacheKey.UPGradeProgressBar + "_" + id; try { if (!msg.Body.StartsWith("hb#")) { bool containsHyphen = msg.Body.Contains("-"); bool isNumeric = msg.Body.All(char.IsDigit); if (containsHyphen == false && isNumeric) { //string hotelcode = Tools.HostNumberToHotelCode(msg.Body).ToString(); //OnOffLineData o = new OnOffLineData(); //o.HotelCode = hotelcode; //o.HostNumber = msg.Body; //o.CurrentStatus = "off"; //o.CurrentTime = DateTime.Now; ////新来的数据 //string str = Newtonsoft.Json.JsonConvert.SerializeObject(o); //CSRedisCacheHelper.redis3.Publish("redis-on_off_line", str); //redis4.Set(HeartBeatPrefix + "_" + msg.Body, 1, 5 * 60); } } } catch (Exception ex) { } }); redis.Subscribe(QQQ); } } /// /// 添加缓存 /// /// /// /// public static void Set(string key, T value, int ExpireTime) { redis?.Set(key, value, ExpireTime * 60); } public static T Get(string key) { return redis.Get(key); } public static void Forever(string key, T value) { redis.Set(key, value, -1); } public static void Del(string key) { redis.Del(key); } public static void Del_Partition(string key, int SliceNo = 2) { CSRedisClient client = WhitchRedisSlice(SliceNo); client.Del(key); } /// /// 判断是否存在 /// /// /// /// public static bool Contains(string key, string mac) { bool result = redis.Exists(mac); if (!result) { result = redis.Exists(key); } return result; } public static int ExpireMinutes = 10; public static T Get_Partition(string key, int SliceNo = 2) { CSRedisClient client = WhitchRedisSlice(SliceNo); return client.Get(key); } public static void Set_Partition(string key, T value, int SliceNo = 2) { CSRedisClient client = WhitchRedisSlice(SliceNo); client.Set(key, value, ExpireMinutes * 60 * 60); } /// /// 设置过期时间 /// /// /// /// /// /// public static void Set_PartitionWithTime(string key, T value, int ExpireTime_Minutes, int SliceNo = 2) { CSRedisClient client = WhitchRedisSlice(SliceNo); client.Set(key, value, ExpireTime_Minutes * 60); } public static void Set_PartitionWithForever(string key, T value, int SliceNo = 2) { CSRedisClient client = WhitchRedisSlice(SliceNo); client.Set(key, value, -1); } public static void Del_Partition(string[] key, int SliceNo = 2) { CSRedisClient client = WhitchRedisSlice(SliceNo); client.Del(key); } public static bool Contains_Partition(string key, int SliceNo = 2) { CSRedisClient client = WhitchRedisSlice(SliceNo); bool result = client.Exists(key); return result; } private static CSRedisClient WhitchRedisSlice(int SliceNo) { CSRedisClient client = null; if (SliceNo == 1) { client = redis1; } else if (SliceNo == 2) { client = redis2; } else if (SliceNo == 3) { client = redis3; } else if (SliceNo == 4) { client = redis4; } else if (SliceNo == 5) { client = redis5; } else { client = redis; } return client; } public static long GetForever_ExpireMinutes(string key) { return redis.Ttl(key); } /// /// 添加Hash缓存 /// /// /// /// public static void HMSet(int SliceNo, string key, params object[] value) { CSRedisClient client = WhitchRedisSlice(SliceNo); client.HMSet(key, value); } public static void HMSet(int SliceNo, int ExpireTime_M, string key, params object[] value) { CSRedisClient client = WhitchRedisSlice(SliceNo); client.HMSet(key, value); client.Expire(key, new TimeSpan(0, ExpireTime_M, 0)); } /// /// 获取Hash缓存 /// /// /// /// public static T[] HMGet(int SliceNo, string key, string KeyV) { CSRedisClient client = WhitchRedisSlice(SliceNo); return client.HMGet(key, KeyV); } public static Dictionary HMGetAll(int SliceNo, string key) { CSRedisClient client = WhitchRedisSlice(SliceNo); return client.HGetAll(key); } /// /// 获取 某个hash值的数量 /// /// /// public static int GetHMCount(string key) { return redis5.HGetAll(key).Count; } /// /// 删除Hash缓存 /// /// /// /// public static long HDelAll(int SliceNo, string key) { CSRedisClient client = WhitchRedisSlice(SliceNo); return client.HDel(key); } public static long HDel(int SliceNo, string key, string key1) { CSRedisClient client = WhitchRedisSlice(SliceNo); return client.HDel(key, key1); } public static void ListAdd(int SliceNo, string key, params object[] obj) { CSRedisClient client = WhitchRedisSlice(SliceNo); client.LPush(key, obj); } public static int MaxLen = 500000; public static void StreamAdd(int SliceNo, string key, string Value) { try { CSRedisClient client = WhitchRedisSlice(SliceNo); // 添加简单消息 client.XAdd(key, MaxLen, "*", new ValueTuple("__data", Value)); } catch (Exception) { } } public static void StreamConsume(int SliceNo, string key, string group = "UDPData", string consumer = "Crics1") { CSRedisClient client = WhitchRedisSlice(SliceNo); var data = redis.XReadGroup(group, consumer, 100, 10, new ValueTuple(key, ">")); if (data != null) { //检查pending表的长度 //消费确认前,pending 应该等于2 var pending = redis.XPending(key, group); foreach (var item in data) { var idarray = item.Item2; foreach (var SerializeNo in idarray) { var id1 = SerializeNo.Item1; redis.XAck(key, group, id1); redis.XDel(key, id1); } } } } } }