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

荒凉一梦 提交于 2019-12-07 06:28:42

问题


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?


回答1:


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




回答2:


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).
}


来源:https://stackoverflow.com/questions/2891868/php-memcache-check-if-any-server-is-available-in-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!