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; private const string ip = "localhost"; private const string port = "26379"; static CSRedisCacheHelper() { var redisHostStr = string.Format("{0}:{1}", ip, port); if (!string.IsNullOrEmpty(redisHostStr)) { redis = new CSRedisClient(redisHostStr + ",password=1001^_^lool,defaultDatabase=0"); var DingYueMsg = ("CellCorelDRAWUser", 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); } public static void LPush(string Key, Dictionary Value) { CSRedisCacheHelper.redis.LPush>(Key,Value); } public static T BRPop(string Key) { return CSRedisCacheHelper.redis.BRPop(3,Key); } /// /// 添加Hash缓存 /// /// /// /// public static void HMSet(string key, params object[] value) { redis.HMSet(key, value); } public static void HMSet(int ExpireTime_M, string key, params object[] value) { redis.HMSet(key, value); redis.Expire(key, new TimeSpan(0, ExpireTime_M, 0)); } /// /// 获取Hash缓存 /// /// /// /// public static T[] HMGet(string key, string KeyV) { return redis.HMGet(key, KeyV); } /// /// Example /// WXFault_酒店编号_房间号 /// /// /// public static Dictionary HMGetAll(string key) { return redis.HGetAll(key); } } }