Memcached::__construct() ([ string $persistent_id
] )
默认情况下,Memcached实例在请求结束后会被销毁。但可以在创建时通过persistent_id
为每个实例指定唯一的ID, 在请求间共享实例。所有通过相同的persistent_id
值创建的实例共享同一个连接。
When using persistent connections, it is important to not re-add servers.
This is what you do not want to do:
<?php
$mc = new Memcached('mc');
$mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$mc->addServers(array(
array('mc1.example.com',11211),
array('mc2.example.com',11211),
));
?>
Every time the page is loaded those servers will be appended to the list resulting in many simultaneous open connections to the same server. The addServer/addServers functions to not check for existing references to the specified servers.
A better approach is something like:
<?php
$mc = new Memcached('mc');
$mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
if (!count($mc->getServerList())) {
$mc->addServers(array(
array('mc1.example.com',11211),
array('mc2.example.com',11211),
));
}
?>