У меня проблема с сохранением Memcached в PHP. Memcached lib возвращает пустой getServerList()
пока не будет 10 одновременных подключений. Не могли найти разумного объяснения этого, но нашли людей с одинаковой проблемой (без решения).
Мой пример:
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
запускается после установления соединений.
Свежий запуск (после перезапуска memcached / apache) показывает:
// 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
Я что-то упускаю? Что происходит?
Моя конфигурация:
Проблема по-прежнему существует в моей последней конфигурации:
Если вы используете php-fpm (или аналогичную конфигурацию с Apache), каждый php-ребенок будет создавать собственное постоянное соединение с memcache. Таким образом, если php-fpm настроен на создание 10 дочерних элементов, то ваш выход в журнал будет иметь смысл 🙂