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