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

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 现在越来越多的开发者使用service-stack.redis 来进行redis的访问,但是获取redisclient的方式有多种方式,其中有一种从缓冲池获取client的方式很是得到大家的认可。 1 List listWrite = new List() { "6380@192.

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

 1  List<string> listWrite = new List<string>() { "6380@192.168.8.245:6380" };
 2             List<string> readHosts = new List<string>() { "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<Phone> phones = redisClient.As<Phone>();
 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  /// <summary>
 2         /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts
 3         /// 返回可以读写的 客户端连接   默认的  使用定义在readWriteHosts中的服务器地址
 4         /// </summary>
 5         /// <returns></returns>
 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 long
18                         if (!Monitor.Wait(writeClients, PoolTimeout.Value))
19                             throw new TimeoutException(PoolTimeoutError);
20                     }
21                     else
22                         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 changed
49                 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的主从复制功能。

 

我又回来了,回到了技术最前线,
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
NoSQL C# Redis
serviceStack.Redis 在PooledRedisClientManager 中设置密码
ServiceStack.Redis 是一个C#访问Redis的客户端,可以说可以通过它实现所有需要Redis-Cli的功能。但是今天我在主Redis 实例设置了访问密码,而在slave 上没有设置,我通过一个缓存工厂来获取连接。
2608 0
|
27天前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?
【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?
|
7天前
|
canal 缓存 NoSQL
Redis缓存与数据库如何保证一致性?同步删除+延时双删+异步监听+多重保障方案
根据对一致性的要求程度,提出多种解决方案:同步删除、同步删除+可靠消息、延时双删、异步监听+可靠消息、多重保障方案
Redis缓存与数据库如何保证一致性?同步删除+延时双删+异步监听+多重保障方案
|
27天前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
|
27天前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Redis 连接失败
【Azure Redis 缓存】Redis 连接失败
|
27天前
|
缓存 NoSQL 网络协议
【Azure Redis 缓存】Lettuce 连接到Azure Redis服务,出现15分钟Timeout问题
【Azure Redis 缓存】Lettuce 连接到Azure Redis服务,出现15分钟Timeout问题
【Azure Redis 缓存】Lettuce 连接到Azure Redis服务,出现15分钟Timeout问题
|
23天前
|
缓存 NoSQL Java
Redis深度解析:解锁高性能缓存的终极武器,让你的应用飞起来
【8月更文挑战第29天】本文从基本概念入手,通过实战示例、原理解析和高级使用技巧,全面讲解Redis这一高性能键值对数据库。Redis基于内存存储,支持多种数据结构,如字符串、列表和哈希表等,常用于数据库、缓存及消息队列。文中详细介绍了如何在Spring Boot项目中集成Redis,并展示了其工作原理、缓存实现方法及高级特性,如事务、发布/订阅、Lua脚本和集群等,帮助读者从入门到精通Redis,大幅提升应用性能与可扩展性。
49 0
|
27天前
|
缓存 NoSQL Redis
【Azure Redis 缓存】使用StackExchange.Redis,偶发ERROR - Timeout performing HSET (15000ms)
【Azure Redis 缓存】使用StackExchange.Redis,偶发ERROR - Timeout performing HSET (15000ms)
|
27天前
|
缓存 NoSQL Java
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
|
8天前
|
存储 NoSQL Redis
SpringCloud基础7——Redis分布式缓存,RDB,AOF持久化+主从+哨兵+分片集群
Redis持久化、RDB和AOF方案、Redis主从集群、哨兵、分片集群、散列插槽、自动手动故障转移
SpringCloud基础7——Redis分布式缓存,RDB,AOF持久化+主从+哨兵+分片集群