using System; using System.Collections.Generic; using System.Linq; using ServiceStack.Redis; namespace CB.Cache.Redis { /// /// 版 本 1.0 /// Copyright (c) 2016-2017 /// 创建人:赵轶 /// 日 期:2016.04.28 10:45 /// 描 述:定义缓存接口 /// public class RedisCache { #region -- 连接信息 -- /// /// redis配置文件信息 /// private static readonly RedisConfigInfo RedisConfigInfo = RedisConfigInfo.GetConfig(); private static PooledRedisClientManager _prcm; /// /// 静态构造方法,初始化链接池管理对象 /// static RedisCache() { CreateManager(); } private RedisCache() { } /// /// 创建链接池管理对象 /// private static void CreateManager() { //写服务地址 string[] writeServerList = SplitString(RedisConfigInfo.WriteServerList, ","); //读服务地址 string[] readServerList = SplitString(RedisConfigInfo.ReadServerList, ","); _prcm = new PooledRedisClientManager(readServerList, writeServerList, new RedisClientManagerConfig { MaxWritePoolSize = RedisConfigInfo.MaxWritePoolSize, MaxReadPoolSize = RedisConfigInfo.MaxReadPoolSize, AutoStart = RedisConfigInfo.AutoStart }); } /// /// 字符串分割 /// /// /// /// private static string[] SplitString(string strSource, string split) { return strSource.Split(split.ToArray()); } #endregion #region -- Item -- /// /// 设置单体 /// /// /// /// /// public static bool Set(string key, T t) { using (IRedisClient redis = _prcm.GetClient()) { return redis.Set(key, t); } } /// /// 设置单体 /// /// /// /// /// /// public static bool Set(string key, T t, TimeSpan timeSpan) { using (IRedisClient redis = _prcm.GetClient()) { return redis.Set(key, t, timeSpan); } } /// /// 设置单体 /// /// /// /// /// /// public static bool Set(string key, T t, DateTime dateTime) { using (IRedisClient redis = _prcm.GetClient()) { return redis.Set(key, t, dateTime); } } /// /// 获取单体 /// /// /// /// public static T Get(string key) where T : class { using (IRedisClient redis = _prcm.GetClient()) { return redis.Get(key); } } /// /// 移除单体 /// /// public static bool Remove(string key) { using (IRedisClient redis = _prcm.GetClient()) { return redis.Remove(key); } } /// /// 清空所有缓存 /// public static void RemoveAll() { using (IRedisClient redis = _prcm.GetClient()) { redis.FlushAll(); } } #endregion #region -- List -- public static void List_Add(string key, T t) { using (IRedisClient redis = _prcm.GetClient()) { var redisTypedClient = redis.As(); redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t); } } public static bool List_Remove(string key, T t) { using (IRedisClient redis = _prcm.GetClient()) { var redisTypedClient = redis.As(); return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > 0; } } public static void List_RemoveAll(string key) { using (IRedisClient redis = _prcm.GetClient()) { var redisTypedClient = redis.As(); redisTypedClient.Lists[key].RemoveAll(); } } public static long List_Count(string key) { using (IRedisClient redis = _prcm.GetClient()) { return redis.GetListCount(key); } } public static List List_GetRange(string key, int start, int count) { using (IRedisClient redis = _prcm.GetClient()) { var c = redis.As(); return c.Lists[key].GetRange(start, start + count - 1); } } public static List List_GetList(string key) { using (IRedisClient redis = _prcm.GetClient()) { var c = redis.As(); return c.Lists[key].GetRange(0, c.Lists[key].Count); } } public static List List_GetList(string key, int pageIndex, int pageSize) { int start = pageSize * (pageIndex - 1); return List_GetRange(key, start, pageSize); } /// /// 设置缓存过期 /// /// /// public static void List_SetExpire(string key, DateTime datetime) { using (IRedisClient redis = _prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } #endregion #region -- Set -- public static void Set_Add(string key, T t) { using (IRedisClient redis = _prcm.GetClient()) { var redisTypedClient = redis.As(); redisTypedClient.Sets[key].Add(t); } } public static bool Set_Contains(string key, T t) { using (IRedisClient redis = _prcm.GetClient()) { var redisTypedClient = redis.As(); return redisTypedClient.Sets[key].Contains(t); } } public static bool Set_Remove(string key, T t) { using (IRedisClient redis = _prcm.GetClient()) { var redisTypedClient = redis.As(); return redisTypedClient.Sets[key].Remove(t); } } #endregion #region -- Hash -- /// /// 判断某个数据是否已经被缓存 /// /// /// /// /// public static bool Hash_Exist(string key, string dataKey) { using (IRedisClient redis = _prcm.GetClient()) { return redis.HashContainsEntry(key, dataKey); } } /// /// 存储数据到hash表 /// /// /// /// /// public static bool Hash_Set(string key, string dataKey, T t) { using (IRedisClient redis = _prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString(t); return redis.SetEntryInHash(key, dataKey, value); } } /// /// 移除hash中的某值 /// /// /// /// /// public static bool Hash_Remove(string key, string dataKey) { using (IRedisClient redis = _prcm.GetClient()) { return redis.RemoveEntryFromHash(key, dataKey); } } /// /// 移除整个hash /// /// /// /// /// public static bool Hash_Remove(string key) { using (IRedisClient redis = _prcm.GetClient()) { return redis.Remove(key); } } /// /// 从hash表获取数据 /// /// /// /// /// public static T Hash_Get(string key, string dataKey) { using (IRedisClient redis = _prcm.GetClient()) { string value = redis.GetValueFromHash(key, dataKey); return ServiceStack.Text.JsonSerializer.DeserializeFromString(value); } } /// /// 获取整个hash的数据 /// /// /// /// public static List Hash_GetAll(string key) { using (IRedisClient redis = _prcm.GetClient()) { var list = redis.GetHashValues(key); if (list != null && list.Count > 0) { List result = new List(); foreach (var item in list) { var value = ServiceStack.Text.JsonSerializer.DeserializeFromString(item); result.Add(value); } return result; } return null; } } /// /// 设置缓存过期 /// /// /// public static void Hash_SetExpire(string key, DateTime datetime) { using (IRedisClient redis = _prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } #endregion #region -- SortedSet -- /// /// 添加数据到 SortedSet /// /// /// /// /// public static bool SortedSet_Add(string key, T t, double score) { using (IRedisClient redis = _prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString(t); return redis.AddItemToSortedSet(key, value, score); } } /// /// 移除数据从SortedSet /// /// /// /// /// public static bool SortedSet_Remove(string key, T t) { using (IRedisClient redis = _prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString(t); return redis.RemoveItemFromSortedSet(key, value); } } /// /// 修剪SortedSet /// /// /// 保留的条数 /// public static long SortedSet_Trim(string key, int size) { using (IRedisClient redis = _prcm.GetClient()) { return redis.RemoveRangeFromSortedSet(key, size, 9999999); } } /// /// 获取SortedSet的长度 /// /// /// public static long SortedSet_Count(string key) { using (IRedisClient redis = _prcm.GetClient()) { return redis.GetSortedSetCount(key); } } /// /// 获取SortedSet的分页数据 /// /// /// /// /// /// public static List SortedSet_GetList(string key, int pageIndex, int pageSize) { using (IRedisClient redis = _prcm.GetClient()) { var list = redis.GetRangeFromSortedSet(key, (pageIndex - 1) * pageSize, pageIndex * pageSize - 1); if (list != null && list.Count > 0) { List result = new List(); foreach (var item in list) { var data = ServiceStack.Text.JsonSerializer.DeserializeFromString(item); result.Add(data); } return result; } } return null; } /// /// 获取SortedSet的全部数据 /// /// /// /// /// /// public static List SortedSet_GetListALL(string key) { using (IRedisClient redis = _prcm.GetClient()) { var list = redis.GetRangeFromSortedSet(key, 0, 9999999); if (list != null && list.Count > 0) { List result = new List(); foreach (var item in list) { var data = ServiceStack.Text.JsonSerializer.DeserializeFromString(item); result.Add(data); } return result; } } return null; } /// /// 设置缓存过期 /// /// /// public static void SortedSet_SetExpire(string key, DateTime datetime) { using (IRedisClient redis = _prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } #endregion } }