note 110003 modified in memcached.addserver by danbrown

From: Date: Wed, 12 Sep 2012 17:29:19 +0000
Subject: note 110003 modified in memcached.addserver by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190664@lists.php.net to get a copy of this message
Important to not call ->addServers() every run -- only call it if no servers exist (check getServerList() ); otherwise, since addServers() does not check for dups, it will let you add the same server again and again and again, resultings in hundreds if not thousands of connections to the MC daemon. Specially when using FastCGI.

Example:

<?php
class Cache {
 private $id;
 private $obj;

 function __construct($id){
 $this->id = $id;
 $this->obj = new Memcached($id);
 }

 public function connect($host , $port){
 $servers = $this->obj->getServerList();
 if(is_array($servers)) {
 foreach ($servers as $server)
 if($server['host'] == $host and $server['port'] == $port)
 return true;
 }
 return $this->obj->addServer($host , $port);
 }

}
?> --was-- Important to not call ->addServers() every run -- only call it if no servers exist (check getServerList() ); otherwise, since addServers() does not check for dups, it will let you add the same server again and again and again, resultings in hundreds if not thousands of connections to the MC daemon. Specially when using FastCGI. Example: class Cache { private $id; private $obj; function __construct($id){ $this->id = $id; $this->obj = new Memcached($id); } public function connect($host , $port){ $servers = $this->obj->getServerList(); if(is_array($servers)) { foreach ($servers as $server) if($server['host'] == $host and $server['port'] == $port) return true; } return $this->obj->addServer($host , $port); } } http://php.net/manual/en/memcached.addserver.php

« previous php.notes (#190664) next »