PHP内存缓存 - 检查池中是否有可用的服务器?

时间:2010-05-23 13:23:52

标签: php memcached libmemcache

我有以下代码:

$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' );
}

我想创建一个函数来检查我的memcache池中的任何服务器是否可用。怎么可以这样做?

2 个答案:

答案 0 :(得分:5)

您可以使用Memcache::getServerStatus检查服务器的状态。

答案 1 :(得分:2)

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