博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Service-stack.redis 使用PooledRedisClientManager 速度慢的原因之一
阅读量:7143 次
发布时间:2019-06-29

本文共 3852 字,大约阅读时间需要 12 分钟。

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

1  List
listWrite = new List
() { "6380@192.168.8.245:6380" }; 2 List
readHosts = new List
() { "192.168.8.245:6381", "192.168.8.245:6382" }; 3 PooledRedisClientManager clientManager = PoolManagerFactory.CreateManager(listWrite.ToArray(), readHosts.ToArray()); 4 ///可以在缓存管理器中加入密码验证 因为没有对应的密码字段显示 5 ///通过getClient获取一个client 连接 6 using (IRedisClient redisClient = clientManager.GetClient()) 7 { 8 IRedisTypedClient
phones = redisClient.As
(); 9 10 Phone phoneFive = phones.GetValue("5");11 if (phoneFive == null)12 {13 phoneFive = new Phone()14 {15 ID = 5,16 Manufacturer = "Nokia",17 Model = "guozhiqi",18 Owner = new Person()19 {20 21 ID = 1,22 Name = "袁金州",23 Surname = "Old"24 }25 };26 phones.SetEntry(phoneFive.ID.ToString(), phoneFive);27 }28 Console.WriteLine("OwnerID" + phones.GetValue("5").Owner.Name);29 }

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

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

1  ///  2         /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts 3         /// 返回可以读写的 客户端连接   默认的  使用定义在readWriteHosts中的服务器地址 4         ///  5         /// 
6 public IRedisClient GetClient() 7 { 8 lock (writeClients) 9 {10 AssertValidReadWritePool();11 12 RedisClient inActiveClient;13 while ((inActiveClient = GetInActiveWriteClient()) == null)14 {15 if (PoolTimeout.HasValue)16 {17 // wait for a connection, cry out if made to wait too long18 if (!Monitor.Wait(writeClients, PoolTimeout.Value))19 throw new TimeoutException(PoolTimeoutError);20 }21 else22 Monitor.Wait(writeClients, RecheckPoolAfterMs);23 }24 25 WritePoolIndex++;26 inActiveClient.Active = true;27 28 if (this.ConnectTimeout != null)29 {30 inActiveClient.ConnectTimeout = this.ConnectTimeout.Value;31 }32 33 if (this.SocketSendTimeout.HasValue)34 {35 inActiveClient.SendTimeout = this.SocketSendTimeout.Value;36 }37 if (this.SocketReceiveTimeout.HasValue)38 {39 inActiveClient.ReceiveTimeout = this.SocketReceiveTimeout.Value;40 }41 if (this.IdleTimeOutSecs.HasValue)42 {43 inActiveClient.IdleTimeOutSecs = this.IdleTimeOutSecs.Value;44 }45 46 inActiveClient.NamespacePrefix = NamespacePrefix;47 48 //Reset database to default if changed49 if (inActiveClient.Db != Db)50 {51 inActiveClient.ChangeDb(Db);52 }53 54 return inActiveClient;55 }56 }

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

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

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

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

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

 

转载地址:http://kpgrl.baihongyu.com/

你可能感兴趣的文章
Redhat linux下cvs的安装配置
查看>>
cxgrid合并值相同的某列
查看>>
增量备份和差异备份的区别
查看>>
纯JS操作获取桌面路径方法
查看>>
thinkphp数据库添加表单提交的数据
查看>>
Hibernate事务属性
查看>>
OVS local network 连通性分析 - 每天5分钟玩转 OpenStack(132)
查看>>
反编译工具jad简单用法
查看>>
无法获取网关MAC地址表/radware备机流量——在不断的应急中提高
查看>>
iOS上使用自定义ttf字体
查看>>
关于CentOS/RHEL 7.x的yum组安装错误的解决方案
查看>>
通过PowerShell轻松转换VHD文件到VHDX格式
查看>>
OLTP应用之MySQL架构选型
查看>>
[Unity插件]LitJson杂谈
查看>>
调节effective_io_concurrenc优化PostgreSQL bitmap index scan性能
查看>>
MySQL体系结构笔记
查看>>
linux常用syslog日志知识
查看>>
VMware试验问题总结
查看>>
说说Micorsoft集群原理
查看>>
Android开发者指南(18) —— Web Apps Overview
查看>>