【问题标题】:PHP memcache - check if any server is available in pool?PHP memcache - 检查池中是否有可用的服务器?
【发布时间】:2010-05-23 13:23:52
【问题描述】:

我有以下代码:

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

我想创建一个函数来检查我的内存缓存池中的任何服务器是否可用。这怎么可能?

【问题讨论】:

    标签: php memcached libmemcache


    【解决方案1】:

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

    【讨论】:

    • 我们最终使用了“memcache::getExtendedStats()”,因为我们发现 getServerStatus 有点不可靠。 getExtendedStatus 似乎总是实时给出服务器的正确状态。
    【解决方案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).
    }
    

    【讨论】:

    • 唯一的问题是,每个失败的服务器可能会增加 1 秒的请求时间(大约),这正是 memcache 应该对抗的那种性能。我建议将超时时间设置为 0.001 或更少……这需要一些实验……
    • @ircmaxell:同意,这就是为什么它是一个变量。仍然Memcache::getServerStatus() 似乎是一个更好的选择,我不知道这种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多