记忆快取效能 [英] Memcached Performance

查看:73
本文介绍了记忆快取效能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我的网站中Memcached的速度比Mysql查询要慢.请查看我从New Relic获得的网站性能的屏幕截图.

I feel the speed of Memcached in my website is slower than Mysql queries. Please see the screenshot of performance of my website I got from New Relic.

我不知道如何在CentOS服务器中优化内存缓存.请查看Memcached的配置和性能屏幕截图.我觉得总连接数"很高.

I don't know how to optimize memcached in my CentOS server. Please see the configuration and performance screenshots of Memcached. I feel the number of Total Connections is high.

请参阅下面的实时统计信息

Please see Live Stats below

以下是我在网站上使用Memcached的方式

The following is how I use Memcached in my website

<?php


class dataCache {

        function setMemData($key, $var, $flag = false, $expire = 36000) {
                global $memcache;
                if (!$memcache) {
                        $memcache = new Memcache;
                        $memcache->connect('localhost', 11211) or die("Could not connect");
                }
                if ($result = $memcache->set($key, $var, $flag, time() + $expire)) {
                        return TRUE;
                } else {
                        return FALSE;
                }
        }

        function getMemData($key) {
                global $memcache;
                if (!$memcache) {
                        $memcache = new Memcache;
                        $memcache->connect('localhost', 11211) or die("Could not connect");
                }

                if ($data = $memcache->get($key)) {
                        return $data;
                } else {
                        return FALSE;
                }
        }

        function delMemData($key) {
                global $memcache;
                if (!$memcache) {
                        $memcache = new Memcache;
                        $memcache->connect('localhost', 11211) or die("Could not connect");
                }

                if ($result = $memcache->delete($key)) {
                        return TRUE;
                } else {
                        return FALSE;
                }
        }
}

在每个PHP页面的结尾,我使用以下方式关闭连接

And at the end of each PHP page, I use the following way to close the connection

if(isset($memcache)){                                                                 
     $memcache->close();
}

此服务器是否需要更多内存?如何减少连接数?有任何改善性能的建议吗?

Do I need more memories for this server? How to reduce the number of connections? Any suggestions to improve the performance?

-------------- EDIT ------------------------

--------------EDIT------------------------

如前所述,当前的连接数为9.总连接数为671731.连接数可能不是问题.

As the comments mentioned, the current connections are 9. The total connections are 671731. The number of connections might be not a problem.

推荐答案

以下是一些使速度更快的方法.

Here are a few ways to make this go faster.

您的总连接数实际上是已创建到memcached的连接数.

Your total connections are actually how many connections have been created to memcached.

首先,使用memcached php扩展名而不是memcache扩展名.它们是完全不同的,并且memcache扩展几乎已被淘汰. Memcached扩展使用libmemcached,它的速度令人难以置信,并且具有更好的功能(例如二进制协议,更好的超时时间,udp)

First, use the memcached php extension NOT the memcache extension. They are entirely different and the memcache extension is pretty much deprecated to death. Memcached extension uses libmemcached which is incredibly faster and has better features (like binary protocol, better timeouts, udp)

第二,使用持久连接.对于您的工作负载,这些应该足够了,并减少了不断重新连接到内存缓存的成本.

Second, use persistent connections. For your workload these should be entirely sufficient and reduce the cost of constantly reconnecting to memcache.

第三,使用多重get/set/delete/etc.如果您知道自己的请求中将需要10个Memcache键,则一次请求全部10个.如果您随时循环遍历内存缓存请求,则可以大大提高性能.

Third, use multi get/set/delete/etc. If you know you will need 10 memcache keys in your request, ask for all 10 at once. This can give you a big performance increase if you are looping over memcache requests at any point.

另一个需要注意的是,从历史上看,NewRelic在报告其在Memcache中所花费的时间时表现不佳.由于使用zend引擎的方式,它可能会将在php中花费的时间错误地报告为在memcache中花费的时间.

Another caveat is that NewRelic is historically BAD at reporting time spent in memcache. It can misreport time spent in php as time spent in memcache due to how the instrument the zend engine.

关于为什么您的内存缓存和mysql性能如此接近的原因,您很可能运行的是相当简单的查询,因此花在内存缓存和mysql上的时间是可比的. Memcache速度极快,但它也是一个网络跃点,通常是等待Memcache所花费的时间最多.如果您确实只有1台服务器,则可以尝试在本地运行memcache或使用APC代替memcache.

As for why your memcache and mysql performance are so close, you are most likely running rather simple queries so the time spent on memcache and on mysql are comparable. Memcache is extremely fast but it is also a network hop and that is usually the largest amount of the time spent waiting for memcache. You could try running memcache locally or using APC instead of memcache if you really only have 1 server.

这篇关于记忆快取效能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆