Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP memcache - check if any server is available in pool?

I have the following code:

$cluster['local'] = array('host' => '192.168.1.1', 'port' => '11211', 'weight' => 50);
$cluster['local2'] = array('host' => '192.168.1.2', 'port' => '11211', 'weight' => 50);

$this->memcache = new Memcache;

foreach ($this->cluster() as $cluster) {
    $this->memcache->addServer($cluster['host'], $cluster['port'], $this->persistent, $cluster['weight'], 10, 10, TRUE , 'failure' );
}

I would like to make a function that checks if any of my servers in my memcache Pool is available. How could this be done?

like image 967
Industrial Avatar asked May 23 '10 13:05

Industrial


2 Answers

You can check on a server's status by using Memcache::getServerStatus.

like image 76
salathe Avatar answered Oct 13 '22 21:10

salathe


Using fsockopen():

$timeout = 1;
$fp = fsockopen('192.168.1.1', 11211,  $errno,  $errstr,  $timeout);

if (is_resource($fp))
{
  // connection to 192.168.1.1:11211 successful
  fclose($fp);
}

else
{
  // failed to connect to 192.168.1.1:11211 within $timeout second(s).
}
like image 43
Alix Axel Avatar answered Oct 13 '22 20:10

Alix Axel