using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using CSRedis; 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"); } } /// /// 添加缓存 /// /// /// /// 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); } } }