using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using CSRedis; namespace BLWWS_BLL.Common { /// /// Redis缓存辅助类 /// public class CSRedisCacheHelper { public static CSRedisClient redis; public static CSRedisClient redis3; 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)) { redis = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1"); redis3 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=3"); //准备存储取电数据 redis5 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=5"); //string channel = "__keyevent@1__:expired"; //var QQQ = new ValueTuple>(channel, (msg) => // { // if (channel.Equals("")) // { // } // Console.WriteLine(msg.MessageId); // Console.WriteLine(msg.Body); // Console.WriteLine("11111111"); // }); //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 bool Contains(string key, string mac) { bool result = redis.Exists(mac); if (!result) { result = redis.Exists(key); } return result; } public static bool Contains(string key) { bool result = redis.Exists(key); return result; } public static void Publish(string Topic, string Payload) { CSRedisCacheHelper.redis3.Publish(Topic, Payload); } } }