using CSRedis; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using static CSRedis.CSRedisClient; namespace Common { /// /// Redis缓存辅助类 /// public class CSRedisCacheHelper { public static CSRedisClient? redis; public static CSRedisClient? redis1; 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=0"); redis1 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1"); var DingYueMsg = ("CallServer", new Action(async (args) => { string body = args.Body; })); CSRedisCacheHelper.redis.Subscribe(DingYueMsg); } } /// /// 添加缓存 /// /// /// /// 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) { bool result = redis.Exists(key); return result; } /// /// 发布消息 /// /// /// public static void Publish(string Topic, string Payload) { CSRedisCacheHelper.redis.PublishNoneMessageId(Topic, Payload); } } }