当前位置: 首页 > news >正文

Service-stack.redis 使用PooledRedisClientManager 速度慢的原因之一

现在越来越多的开发者使用service-stack.redis 来进行redis的访问,但是获取redisclient的方式有多种方式,其中有一种从缓冲池获取client的方式很是得到大家的认可。

List<string> listWrite = new List<string>() { "6380@192.168.8.245:6380" };
            List<string> readHosts = new List<string>() { "192.168.8.245:6381", "192.168.8.245:6382" };
            PooledRedisClientManager clientManager = PoolManagerFactory.CreateManager(listWrite.ToArray(), readHosts.ToArray());
            ///可以在缓存管理器中加入密码验证  因为没有对应的密码字段显示
          ///通过getClient获取一个client 连接
            using (IRedisClient redisClient = clientManager.GetClient())
            {
                IRedisTypedClient<Phone> phones = redisClient.As<Phone>();
                
                Phone phoneFive = phones.GetValue("5");
                if (phoneFive == null)
                {
                    phoneFive = new Phone()
                    {
                        ID = 5,
                        Manufacturer = "Nokia",
                        Model = "guozhiqi",
                        Owner = new Person()
                        {

                            ID = 1,
                            Name = "袁金州",
                            Surname = "Old"
                        }
                    };
                    phones.SetEntry(phoneFive.ID.ToString(), phoneFive);
                }
                Console.WriteLine("OwnerID" + phones.GetValue("5").Owner.Name);
            }

请注意上面代码的第五行,using (IRedisClient redisClient = clientManager.GetClient()){}

通过clientManager.getClient方法来获取一个连接,我们在ado.net中也是采用这种方式,而且性能很高。我们认为这种方式的工作方式肯定是首先从缓冲池中获取一条连接,然后执行using里面的代码,最后dispose。但是有时候这种方式在稍微访问量大的时候性能很低,什么原因呢?

/// <summary>
        /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts
        /// 返回可以读写的 客户端连接   默认的  使用定义在readWriteHosts中的服务器地址
        /// </summary>
        /// <returns></returns>
        public IRedisClient GetClient()
        {
            lock (writeClients)
            {
                AssertValidReadWritePool();

                RedisClient inActiveClient;
                while ((inActiveClient = GetInActiveWriteClient()) == null)
                {
                    if (PoolTimeout.HasValue)
                    {
                        // wait for a connection, cry out if made to wait too long
                        if (!Monitor.Wait(writeClients, PoolTimeout.Value))
                            throw new TimeoutException(PoolTimeoutError);
                    }
                    else
                        Monitor.Wait(writeClients, RecheckPoolAfterMs);
                }

                WritePoolIndex++;
                inActiveClient.Active = true;

                if (this.ConnectTimeout != null)
                {
                    inActiveClient.ConnectTimeout = this.ConnectTimeout.Value;
                }

                if (this.SocketSendTimeout.HasValue)
                {
                    inActiveClient.SendTimeout = this.SocketSendTimeout.Value;
                }
                if (this.SocketReceiveTimeout.HasValue)
                {
                    inActiveClient.ReceiveTimeout = this.SocketReceiveTimeout.Value;
                }
                if (this.IdleTimeOutSecs.HasValue)
                {
                    inActiveClient.IdleTimeOutSecs = this.IdleTimeOutSecs.Value;
                }

                inActiveClient.NamespacePrefix = NamespacePrefix;

                //Reset database to default if changed
                if (inActiveClient.Db != Db)
                {
                    inActiveClient.ChangeDb(Db);
                }

                return inActiveClient;
            }
        }

这是service-stack.redis中getClient的实现,但是我们发现了一个问题就是,他只从主 redis中获取连接,不可能返回slave 的readonly 连接。

如果缓存设置为5,那么如果同时500个请求,还是会有性能影响的,因为完全忽略了slave的读的功能。

如果要写,我们可以调用clientManager.GetClient() 来获取writeHosts的redis实例。

如果要读,我们可以调用clientManager.GetReadOnlyClient()来获取仅仅是readonlyHost的redis实例。

如果你嫌麻烦,那么完全可以使用clientManager.GetCacheClient() 来获取一个连接,他会在写的时候调用GetClient获取连接,读的时候调用GetReadOnlyClient获取连接,这样可以做到读写分离,从而利用redis的主从复制功能。

相关文章:

  • [Linux] - 定时任务crontab
  • 30分钟让你了解MongoDB基本操作(转)
  • There is already an open DataReader associated with this Command which must be closed first
  • 部署Exchange2013
  • Windows server 2008 R2配置路由和***
  • 业务
  • OK335x mksd.sh hacking
  • c#获取url请求的返回值(转)
  • ios开发 xcode6以上安装Alcatraz管理插件
  • 部署与管理ZooKeeper
  • linux内核移植X86平台的例子
  • 【BZOJ】3526: [Poi2014]Card
  • 解决mysql查询中文乱码问题
  • js字符串 数组处理
  • Redhat编译php-5.2.9出现error dereferencing pointer to incomplete type
  • #Java异常处理
  •  D - 粉碎叛乱F - 其他起义
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • jquery cookie
  • JS变量作用域
  • JWT究竟是什么呢?
  • Laravel Telescope:优雅的应用调试工具
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • Redis在Web项目中的应用与实践
  • ucore操作系统实验笔记 - 重新理解中断
  • V4L2视频输入框架概述
  • 关于List、List?、ListObject的区别
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 线上 python http server profile 实践
  • 智能网联汽车信息安全
  • No resource identifier found for attribute,RxJava之zip操作符
  • 湖北分布式智能数据采集方法有哪些?
  • 我们雇佣了一只大猴子...
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ​一、什么是射频识别?二、射频识别系统组成及工作原理三、射频识别系统分类四、RFID与物联网​
  • # Panda3d 碰撞检测系统介绍
  • #、%和$符号在OGNL表达式中经常出现
  • #14vue3生成表单并跳转到外部地址的方式
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • ${factoryList }后面有空格不影响
  • (2)(2.10) LTM telemetry
  • (2)nginx 安装、启停
  • (6)STL算法之转换
  • (C++)八皇后问题
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (八)Spring源码解析:Spring MVC
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • ***利用Ms05002溢出找“肉鸡
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .jks文件(JAVA KeyStore)
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .Net MVC + EF搭建学生管理系统
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?