设为首页 收藏本站
查看: 679|回复: 0

[经验分享] Redis操作的封装类

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-21 13:00:12 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
      
namespace Com.QFGame.QNX.Community.Redis
{
    public class RedisBase
    {
      
        private static string RedisPath = System.Configuration.ConfigurationSettings.AppSettings["RedisPath"];
      
        #region -- 连接信息 --
        //10.0.18.8:6379
        public static PooledRedisClientManager prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath });
        private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
        {  
            // 支持读写分离,均衡负载  
            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
            {
                MaxWritePoolSize = 5, // “写”链接池链接数  
                MaxReadPoolSize = 5, // “读”链接池链接数  
                AutoStart = true,
            });
        }
        #endregion
      
      
      
      
      
      
      
      
      
      
        #region -- Item --
        ///  
        /// 设置单体
        ///  
        ///  
        ///  
        ///  
        ///  
        ///  
        public static bool Item_Set(string key, T t)
        {
            try
            {
                using (IRedisClient redis = prcm.GetClient())
                {
                    return redis.Set(key, t, new TimeSpan(1, 0, 0));
                }
            }
            catch (Exception ex)
            {
                // LogInfo
            }
            return false;
        }
      
        ///  
        /// 获取单体
        ///  
        ///  
        ///  
        ///  
        public static T Item_Get(string key) where T : class
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                return redis.Get(key);
            }
        }
      
        ///  
        /// 移除单体
        ///  
        ///  
        public static bool Item_Remove(string key)
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                return redis.Remove(key);
            }
        }
      
        #endregion
      
        #region -- List --
      
        public static void List_Add(string key, T t)
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                var redisTypedClient = redis.GetTypedClient();
                redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t);
            }
        }
      
               
      
        public static bool List_Remove(string key, T t)
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                var redisTypedClient = redis.GetTypedClient();
                return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > 0;
            }
        }
        public static void List_RemoveAll(string key)
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                var redisTypedClient = redis.GetTypedClient();
                redisTypedClient.Lists[key].RemoveAll();
            }
        }
      
        public static int 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.GetTypedClient();
                return c.Lists[key].GetRange(start, start + count - 1);
            }
        }
      
      
        public static List List_GetList(string key)
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                var c = redis.GetTypedClient();
                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.GetTypedClient();
                redisTypedClient.Sets[key].Add(t);
            }
        }
        public static bool Set_Contains(string key, T t)
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                var redisTypedClient = redis.GetTypedClient();
                return redisTypedClient.Sets[key].Contains(t);
            }
        }
        public static bool Set_Remove(string key, T t)
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                var redisTypedClient = redis.GetTypedClient();
                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 int SortedSet_Trim(string key, int size)
        {
            using (IRedisClient redis = prcm.GetClient())
            {
                return redis.RemoveRangeFromSortedSet(key, size, 9999999);
            }
        }
        ///  
        /// 获取SortedSet的长度
        ///  
        ///  
        ///  
        public static int 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);
            }
        }
      
        //public static double SortedSet_GetItemScore(string key,T t)
        //{
        //    using (IRedisClient redis = prcm.GetClient())
        //    {
        //        var data = ServiceStack.Text.JsonSerializer.SerializeToString(t);
        //        return redis.GetItemScoreInSortedSet(key, data);
        //    }
        //    return 0;
        //}
      
        #endregion
      
    }
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-89098-1-1.html 上篇帖子: Redis系列 下篇帖子: Redis 起步(linux)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表