MicronXD MicronXD - 5 months ago 10
PHP Question

Memcached PHP module: How can you tell if the connection failed?

in a php file, I have:

<?php
$m = new Memcached();
echo get_class($m);
echo "<br>";
echo $m->addServer('192.168.1.77', 11211, 1);
$m->set("foo", "bar");
?>


when run, around a half second later, I get:

Memcached
1


If I stop memcached, after about 5 seconds, I get:

Memcached
1


I was expecting something more like...

Memcached
0


How do you know if you've successfully hit the memcached server or not? I was fully expecting it to be as simple as a return value from addServer... :(

Answer

This is kinda what I was looking for: Memcached::getStats();

So, I wrote:

add_memcached_server($m, $addr, $port)
{
    $m->addServer($addr,$port);
    $statuses = $m->getStats();
    return isset($statuses[$addr.":".$port]);
}

works like a charm...