基本信息
源码名称:NServiceKit.Redis 基础操作
源码大小:0.99M
文件格式:.zip
开发语言:C#
更新时间:2017-07-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍
 /// <summary>
        /// redis客户端连接池信息
        /// </summary>
        private PooledRedisClientManager prcm;

        public Redis()
        {
            CreateManager();

        }
        /// <summary>
        /// 创建链接池管理对象
        /// </summary>
        private void CreateManager()
        {
            try
            {
                // ip1:端口1,ip2:端口2
                var serverlist = ConfigurationManager.AppSettings["RedisServer"].Split(',');
                prcm = new PooledRedisClientManager(serverlist, serverlist,
                                 new RedisClientManagerConfig
                                 {
                                     MaxWritePoolSize = 32,
                                     MaxReadPoolSize = 32,
                                     AutoStart = true
                                 });
                // prcm.Start();
            }
            catch (Exception e)
            {
#if DEBUG
                throw;
#endif
            }
        }
        /// <summary>
        /// 客户端缓存操作对象
        /// </summary>
        public IRedisClient GetClient()
        {
            if (prcm == null)
                CreateManager();

            return prcm.GetClient();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(string key)
        {
            using (var client = prcm.GetClient())
            {
                return client.Remove(key);
            }
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object Get(string key)
        {
            using (var client = prcm.GetClient())
            {
                var bytes = client.Get<byte[]>(key);
                var obj = new ObjectSerializer().Deserialize(bytes);
                return obj;
            }
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetT<T>(string key) where T : class
        {
            //return Get(key) as T;
            using (var client = prcm.GetClient())
            {
               return client.Get<T>(key);
            }
        }
        /// <summary>
        /// 获取到值到内存中,在删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object GetWithDelete(string key)
        {
            var result = Get(key);
            if (result != null)
                Remove(key);
            return result;
        }
        /// <summary>
        /// 获取到值到内存中,在删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetWithDelete<T>(string key) where T : class
        {
            var result = GetT<T>(key);
            if (result != null)
                Remove(key);
            return result;
        }
        /// <summary>
        /// 写
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Set(string key, object value)
        {
            using (var client = prcm.GetClient())
            {
                if (client.ContainsKey(key))
                {
                    return client.Set<byte[]>(key, new ObjectSerializer().Serialize(value));
                }
                else
                {
                    return client.Add<byte[]>(key, new ObjectSerializer().Serialize(value));
                }
            }

        }
        /// <summary>
        /// 写
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expireTime"></param>
        /// <returns></returns>
        public bool Set(string key, object value, DateTime expireTime)
        {
            using (var client = prcm.GetClient())
            {
                if (client.ContainsKey(key))
                {
                    return client.Set<byte[]>(key, new ObjectSerializer().Serialize(value), expireTime);
                }
                else
                {
                    return client.Add<byte[]>(key, new ObjectSerializer().Serialize(value), expireTime);
                }

            }
        }
        /// <summary>
        /// 写
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expire"></param>
        /// <returns></returns>
        public bool SetT<T>(string key, T value, DateTime expire) where T : class
        {
            try
            {
                using (var client = prcm.GetClient())
                {
                    return client.Set<T>(key, value, expire);
                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 写
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetT<T>(string key, T value) where T : class
        {
            try
            {
                using (var client = prcm.GetClient())
                {
                    return client.Set<T>(key, value);
                }
            }
            catch
            {
                return false;
            }
        }

        public void Dispose()
        {
            Close();
        }

        public void Close()
        {
            var client = prcm.GetClient();
            prcm.Dispose();
        }