I'm trying to get Memcached persistent connections to work. We currently have a cluster of four Memcached servers each with 2GB of RAM, and four web servers. My code looks like this:
public function __construct()
{
global $config;
$this->cache = new Memcached('daniweb');
if ($this->cache->isPristine())
{
$this->cache->setOptions(array(
Memcached::OPT_NO_BLOCK => true,
Memcached::OPT_BUFFER_WRITES => true,
Memcached::OPT_BINARY_PROTOCOL => true,
Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
));
}
if (count($this->cache->getServerList()) == 0)
{
// Connect to all memcache servers
$this->cache->addServers($config['Cache']['memcacheserver']);
}
}
public function __destruct()
{
$this->cache->resetServerList();
}
If I don't resetServerList()
, then the number of connections keeps going up and up until it reaches max allowed connections and freezes the servers. Why does this happen? I was under the impression that if you pass in a persistent ID into the Memcached constructor, it reuses the open connection. I'm only telling it to add servers to the connection pool if the connection pool is currently empty. What am I doing wrong??