A widely circulated online article about PHP's two expansion modules Memcache and memcached differences, which deliberately highlighted the memcached and memcached a big difference is that the memcached module does not support long connections. So many years later I think memcached is not support long connection, in fact, memcached Extension module from the early version has been supporting long connection. From the source of the extension module, we can see:
/* {{{memcached::__construct ([string persistent_id[, Callback on_new[, String Connection_str]])
Creates a Memcached object, optionally using persistent memcache connection * *
Static Php_method (Memcached, __construct)
{
From the PHP manual, we can see that the constructor provided by the Memcached Extension module provides a persistent_id parameter option, as described in the manual:
By default, the Memcached instance is destroyed after the request is completed. However, you can share an persistent_id instance between requests at creation time by assigning a unique ID to each instance. All instances created with persistent_id the same value share the same connection.
The implication of this argument is that if you pass a named ID to the construction method, then a long connection is established, and usually we use PHP-FPM mode, so that the PHP-FPM process will be connected to the Memcached service resume a long connection channel. We can also understand that persistent_id is a connection pool name, and all PHP-FPM processes are part of this connection pool.
But what we need to be aware of is that PHP is an interpretive language, and when PHP first establishes a long connection through the Memached module, remember that subsequent PHP execution does not build the same persistent_id named long connection through the Memcached constructor, You can build a long connection with different persistent_id names, and if the same name is repeated by PHP, it will lead to PHP-FPM process anomalies that lead to slower communication with memcached, Also, depending on the version of libmemcached, PHP generates Coredump.
So how do we prevent a single php-fpm from creating long connections after a long connection named after persistent_id? In fact, PHP has a commentary on the manual is explained, the contents are as follows:
When using persistent connections, the It is important to not re-add servers.
This is what you don't want to do:
$MC = new Memcached (' MC ');
$MC->setoption (memcached::opt_libketama_compatible, true);
$MC->addservers (Array (
' mc1.example.com ', 11211),
array (' mc2.example.com ', 11211))
;
Every time the page is loaded those servers'll be appended to the list resulting in many simultaneous open connections T o the same server. The Addserver/addservers functions to don't check for existing references to the specified servers.
A better approach is something like:
$MC = new Memcached (' MC ');
$MC->setoption (memcached::opt_libketama_compatible, true);
if (!count ($MC->getserverlist ())) {
$MC->addservers (Array (
' mc1.example.com ', 11211),
Array (' mc2.example.com ', 11211)
;
}
Use the Getserverlist () method to check whether a long connection resource with the same name already exists in the PHP-FPM process container that is currently executing, and do not reuse the Addservers () method to new growth of the connection configuration if it exists.