PHP中的永久内存缓存-服务器池不断增长,直到curr_connections 10 [英] Persistent memcached in PHP - server pool growing until curr_connections 10

查看:109
本文介绍了PHP中的永久内存缓存-服务器池不断增长,直到curr_connections 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP中的Memcached持久性有疑问. Memcached lib返回空的getServerList(),直到有10个并发连接.找不到合理的解释,但发现有相同问题的人(没有解决方案).

I have a problem with Memcached persistence in PHP. Memcached lib returns empty getServerList() until there are 10 concurrent connections. Couldn't find sensible explanation of that, but found people with the same problem (without solution).

我的例子:

class Cache {

    protected $memcached;

    public function __construct($cache_name) {
        $instance_name = 'persistent-' . $cache_name;
        $this->memcached = new Memcached($instance_name);
        $server_count = count($this->memcached->getServerList());
        echo '[MC] Server count of ', $instance_name, ': ', $server_count, PHP_EOL;

        if (0 == $server_count) {
            $servers = array(array("localhost","16586"),array("localhost","16587"));
            echo '[MC] Adding servers: ', json_encode($servers), PHP_EOL;
            // options don't change anything in my case
            $this->memcached->setOptions(array(
                Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
                Memcached::OPT_LIBKETAMA_COMPATIBLE => true
            ));
            $this->memcached->addServers($servers);
        }

        $stats = $this->memcached->getStats();
        foreach($stats as $server => $data){
            echo '[MC] Stats of ', $server, ' curr_connections: ', $data['curr_connections'], ' total_connections: ', $data['total_connections'], PHP_EOL;
        }
    }

    public function get($key) {
        echo '[MC] Server for key: ', $key, ' is ', json_encode($this->memcached->getServerByKey($key)), PHP_EOL;
        $ret = $this->memcached->get($key);
        echo '[MC] Getting ', $key, ' with result: ', $ret, PHP_EOL;
        return $ret;
    }

    public function set($key, $data, $timeout = 0) {
        echo '[MC] Set of ', $key, ' with data: ', $data, PHP_EOL;
        return $this->memcached->set($key, $data, $timeout);
    }
}

$cache = new Cache('instance');

$ret = $cache->get('something');

if(empty($ret)) {
    $cache->set('something', true);
}

我希望这段代码是在建立连接后运行一次addServers.

What I expect from this code is single addServers run after connections are established.

新运行(在memcached/apache重新启动后)显示:

Fresh run (after memcached/apache restart) shows:

// first run
[MC] Server count of persistent-instance: 0
[MC] Adding servers: [["localhost","16586"],["localhost","16587"]]
[MC] Stats of localhost:16586 curr_connections: 5 total_connections: 6
[MC] Stats of localhost:16587 curr_connections: 5 total_connections: 6
[MC] Server for key: something is {"host":"localhost","port":16587,"weight":1}
[MC] Getting something with result: 
[MC] Set of something with data: 1

// second
[MC] Server count of persistent-instance: 0
[MC] Adding servers: [["localhost","16586"],["localhost","16587"]]
[MC] Stats of localhost:16586 curr_connections: 6 total_connections: 7
[MC] Stats of localhost:16587 curr_connections: 6 total_connections: 7
[MC] Server for key: something is {"host":"localhost","port":16587,"weight":1}
[MC] Getting something with result: 1

// up to 6th call curr_connections are growing and still adding servers to pool
[MC] Server count of persistent-instance: 0
[MC] Adding servers: [["localhost","16586"],["localhost","16587"]]
[MC] Stats of localhost:16586 curr_connections: 10 total_connections: 11
[MC] Stats of localhost:16587 curr_connections: 10 total_connections: 11
[MC] Server for key: something is {"host":"localhost","port":16587,"weight":1}
[MC] Getting something with result: 1

// 7th+ call finally with expected result
[MC] Server count of persistent-instance: 2
[MC] Stats of localhost:16586 curr_connections: 10 total_connections: 11
[MC] Stats of localhost:16587 curr_connections: 10 total_connections: 11
[MC] Server for key: something is {"host":"localhost","port":16587,"weight":1}
[MC] Getting something with result: 1

我错过了什么吗?发生什么事了?

Am I missing something? What's happening?

我的配置:

  • Ubuntu 13.04
  • Apache 2.2.22
  • Memcached服务器1.4.14(4个实例)
  • libmemcached 1.0.8
  • PHP 5.4.9-4ubuntu2.3
  • 用于PHP的Memcached库最新.
  • Ubuntu 13.04
  • Apache 2.2.22
  • Memcached server 1.4.14 (4 instances)
  • libmemcached 1.0.8
  • PHP 5.4.9-4ubuntu2.3
  • Memcached lib for PHP latest.

问题仍然存在于我的最新配置中:

The problem still exists on my latest config:

  • Xubuntu 13.10(内核3.11.0-19)
  • Apache 2.4.6
  • Memcached 1.4.14
  • libmemcached 1.0.8
  • PHP 5.5.3-1ubuntu2.3
  • 用于PHP 2.1.0的Memcached库

推荐答案

如果您使用的是php-fpm(或与Apache类似的配置),则每个php子代都将创建其自己的与内存缓存的持久连接.因此,如果将php-fpm配置为创建10个子代,则您的日志输出完全有意义:-)

If you are using php-fpm (or a similar config with Apache), each php child will create its own persistent connection to memcache. So if php-fpm is configured to create 10 child, your log output makes fully sense :-)

这篇关于PHP中的永久内存缓存-服务器池不断增长,直到curr_connections 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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