• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#RedisHelper

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

使用redis组件如下,至于为什么使用3.9版本,是因为4.0开始商业了,限制了次数

ServiceStack.Common" version="3.9.70"
ServiceStack.Redis" version="3.9.71"
ServiceStack.Text" version="3.9.71"

接口

 public interface ICache
    {
        #region Key-Value
        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        T Read<T>(string cacheKey, long dbId = 0) where T : class;
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="value">对象数据</param>
        /// <param name="cacheKey"></param>
        void Write<T>(string cacheKey, T value, long dbId = 0) where T : class;
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="value">对象数据</param>
        /// <param name="cacheKey"></param>
        /// <param name="expireTime">到期时间</param>
        void Write<T>(string cacheKey, T value, DateTime expireTime, long dbId = 0) where T : class;
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="value">对象数据</param>
        /// <param name="cacheKey"></param>
        /// <param name="expireTime">到期时间</param>
        void Write<T>(string cacheKey, T value, TimeSpan timeSpan, long dbId = 0) where T : class;
        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        void Remove(string cacheKey, long dbId = 0);
        /// <summary>
        /// 移除全部缓存
        /// </summary>
        void RemoveAll(long dbId = 0);
        #endregion
    }
View Code

配置类

public sealed class RedisConfigInfo : ConfigurationSection
    {
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <returns></returns>
        public static RedisConfigInfo GetConfig()
        {
            return GetConfig("redisconfig");
        }
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="sectionName">xml节点名称</param>
        /// <returns></returns>
        public static RedisConfigInfo GetConfig(string sectionName)
        {
            RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection(sectionName);
            if (section == null)
                throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
            return section;
        }
        /// <summary>
        /// 可写的Redis链接地址
        /// </summary>
        [ConfigurationProperty("WriteServerList", IsRequired = false)]
        public string WriteServerList
        {
            get
            {
                return (string)base["WriteServerList"];
            }
            set
            {
                base["WriteServerList"] = value;
            }
        }
        /// <summary>
        /// 可读的Redis链接地址
        /// </summary>
        [ConfigurationProperty("ReadServerList", IsRequired = false)]
        public string ReadServerList
        {
            get
            {
                return (string)base["ReadServerList"];
            }
            set
            {
                base["ReadServerList"] = value;
            }
        }
        /// <summary>
        /// 最大写链接数
        /// </summary>
        [ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)]
        public int MaxWritePoolSize
        {
            get
            {
                int _maxWritePoolSize = (int)base["MaxWritePoolSize"];
                return _maxWritePoolSize > 0 ? _maxWritePoolSize : 5;
            }
            set
            {
                base["MaxWritePoolSize"] = value;
            }
        }
        /// <summary>
        /// 最大读链接数
        /// </summary>
        [ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)]
        public int MaxReadPoolSize
        {
            get
            {
                int _maxReadPoolSize = (int)base["MaxReadPoolSize"];
                return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5;
            }
            set
            {
                base["MaxReadPoolSize"] = value;
            }
        }
        /// <summary>
        /// 自动重启
        /// </summary>
        [ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)]
        public bool AutoStart
        {
            get
            {
                return (bool)base["AutoStart"];
            }
            set
            {
                base["AutoStart"] = value;
            }
        }
        /// <summary>
        /// 本地缓存到期时间,单位:秒
        /// </summary>
        [ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = 36000)]
        public int LocalCacheTime
        {
            get
            {
                return (int)base["LocalCacheTime"];
            }
            set
            {
                base["LocalCacheTime"] = value;
            }
        }
        /// <summary>
        /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
        /// </summary>
        [ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)]
        public bool RecordeLog
        {
            get
            {
                return (bool)base["RecordeLog"];
            }
            set
            {
                base["RecordeLog"] = value;
            }
        }
        /// <summary>
        /// 默认开始db
        /// </summary>
        [ConfigurationProperty("DefaultDb", IsRequired = false)]
        public long DefaultDb
        {
            get
            {
                return (long)base["DefaultDb"];
            }
            set
            {
                base["DefaultDb"] = value;
            }
        }

    }
View Code

处理类

 public class RedisCache
    {
        #region -- 连接信息 --
        /// <summary>
        /// redis配置文件信息
        /// </summary>
        private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();
        /// <summary>
        /// 创建链接池管理对象
        /// </summary>
        private static PooledRedisClientManager CreateManager(long dbId)
        {
            string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
            string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");

            return new PooledRedisClientManager(readServerList, writeServerList,
                             new RedisClientManagerConfig
                             {
                                 MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
                                 MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
                                 AutoStart = redisConfigInfo.AutoStart,
                                 DefaultDb = dbId
                             });
        }
        /// <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());
        }
        /// <summary>
        /// 获取redis客户端根据库ID号
        /// </summary>
        /// <param name="dbId">redis库Id</param>
        /// <returns></returns>
        private static PooledRedisClientManager GetClientManager(long dbId)
        {
            return CreateManager(dbId);
        }

        #endregion

        #region -- Item --
        /// <summary>
        /// 设置单体
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="t"></param>
        /// <param name="dbId">库Id</param>
        /// <returns></returns>
        public static bool Set<T>(string key, T t, long dbId = 0)
        {
            var clientManager = GetClientManager(dbId);
            IRedisClient redis = clientManager.GetClient();
            var res = redis.Set<T>(key, t);
            clientManager.DisposeClient((RedisNativeClient)redis);
            redis.Dispose();
            clientManager.Dispose();
            return res;
        }
        /// <summary>
        /// 设置单体
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="t"></param>
        /// <param name="timeSpan">保存时间</param>
        /// <param name="dbId">库Id</param>
        /// <returns></returns>
        public static bool Set<T>(string key, T t, TimeSpan timeSpan, long dbId = 0)
        {
            var clientManager = GetClientManager(dbId);
            IRedisClient redis = clientManager.GetClient();
            var res = redis.Set<T>(key, t, timeSpan);
            clientManager.DisposeClient((RedisNativeClient)redis);
            redis.Dispose();
            clientManager.Dispose();
            return res;
        }
        /// <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, long dbId = 0)
        {
            var clientManager = GetClientManager(dbId);
            IRedisClient redis = clientManager.GetClient();
            var res = redis.Set<T>(key, t, dateTime);
            clientManager.DisposeClient((RedisNativeClient)redis);
            redis.Dispose();
            clientManager.Dispose();
            return res;
        }

        /// <summary>
        /// 获取单体
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static T Get<T>(string key, long dbId = 0) where T : class
        {
            var clientManager = GetClientManager(dbId);
            IRedisClient redis = clientManager.GetClient();
            var res = redis.Get<T>(key);
            clientManager.DisposeClient((RedisNativeClient)redis);
            redis.Dispose();
            clientManager.Dispose();
            return res;
        }
        /// <summary>
        /// 移除单体
        /// </summary>
        /// <param name="key">键值</param>
        public static bool Remove(string key, long dbId = 0)
        {
            var clientManager = GetClientManager(dbId);
            IRedisClient redis = clientManager.GetClient();
            var res = redis.Remove(key);
            clientManager.DisposeClient((RedisNativeClient)redis);
            redis.Dispose();
            clientManager.Dispose();
            return res;
        }
        /// <summary>
        /// 清空所有缓存
        /// </summary>
        public static void RemoveAll(long dbId = 0)
        {
            var clientManager = GetClientManager(dbId);
            IRedisClient redis = clientManager.GetClient();
            redis.FlushDb();
            clientManager.DisposeClient((RedisNativeClient)redis);
            redis.Dispose();
            clientManager.Dispose();
        }
        #endregion

        #region -- List --
        /// <summary>
        /// 添加列表
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="t"></param>
        /// <param name="dbId"></param>
        public static void List_Add<T>(string key, T t, long dbId = 0)
        {
            using (IRedisClient redis = CreateManager(dbId).GetClient())
            {
                var redisTypedClient = redis.As<T>();
                redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t);
            }
        }
        /// <summary>
        /// 移除列表某个值
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="t"></param>
        /// <param name="dbId"></param>
        /// <returns></returns>
        public static bool List_Remove<T>(string key, T t, long dbId = 0)
        {
            using (IRedisClient redis = CreateManager(dbId).GetClient())
            {
                var redisTypedClient = redis.As<T>();
                return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > 0;
            }
        }
        /// <summary>
        /// 移除列表所有值
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="dbId">库Id</param>
        public static void List_RemoveAll<T>(string key, long dbId = 0)
        {
            using (IRedisClient redis = CreateManager(dbId).GetClient())
            {
                var redisTypedClient = redis.As<T>();
                redisTypedClient.Lists[key].RemoveAll();
            }
        }
        /// <summary>
        /// 获取列表数据条数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dbId"></param>
        /// <returns></returns>
        public static long List_Count(string key, long dbId = 0)
        {
            using (IRedisClient redis = CreateManager(dbId).GetClient())
            {
                return redis.GetListCount(key);
            }
        }
        /// <summary>
        /// 获取指定条数列表数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="start">开始编号</param>
        /// <param name="count">条数</param>
        /// <param name="dbId"></param>
        /// <returns></returns>
        public static List<T> List_GetRange<T>(string key, int start, int count, long dbId = 0)
        {
            using (IRedisClient redis = CreateManager(dbId).GetClient())
            {
                var c = redis.As<T>();
                return c.Lists[key].GetRange(start, start + count - 1);
            }
        }
        /// <summary>
        /// 获取列表所有数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="dbId">库数据</param>
        /// <returns></returns>
        public static List<T> List_GetList<T>(string key, long dbId = 0)
        {
            using (IRedisClient redis = CreateManager(dbId).GetClient())
            {
                var c = redis.As<T>();
                return c.Lists[key].GetRange(0, c.Lists[key].Count);
            }
        }
        /// <summary>
        /// 获取列表分页数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="dbId"></param>
        /// <returns></returns>
        public static List<T> List_GetList<T>(string key, int pageIndex, int pageSize, long dbId = 0)
        {
            int start = pageSize * (pageIndex - 1);
            return List_GetRange<T>(key, start, pageSize, dbId);
        }
        #endregion

        #region -- Set --
        /// <summary>
        /// 添加集合
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="t">数值</param>
        /// <param name="dbId"></param>
        public static void Set_Add<T>(string key, T t, long dbId = 0)
        {
            using (IRedisClient redis = CreateManager(dbId).GetClient())
            {
                var redisTypedClient = redis.As<T>();
                redisTypedClient.Sets[key].Add(t);
            }
        }
        /// <summary>
        /// 集合是否包含指定数据
         
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
NX二次开发-UfunC函数例子目录【更新日期2022.4.16】发布时间:2022-07-14
下一篇:
C/C++中的预编译指令(转)发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap