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

现在越来越多的开发者使用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的主从复制功能。

 

posted @ 2014-10-14 15:55  baidixing  阅读(10917)  评论(1编辑  收藏  举报