zoukankan      html  css  js  c++  java
  • 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的主从复制功能。

  • 相关阅读:
    提高github下载速度
    小程序兼容问题
    求斐波拉契数列第n位算法优化
    并发编程:ThreadLocal
    MySQL:常见面试题
    2.1语法基础_表达式目录树(EF底层原理的实现)
    ajax发送post请求:
    投资是普通人摆脱阶层固化的唯一靠谱方式
    人脸识别之Python基于OpenCV
    搜索算法“一二”基于VSCode平台C#语言
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/4950143.html
Copyright © 2011-2022 走看看