tim peterson
tim peterson

Reputation: 24305

Check if memcache connection exists using PHP?

According to php.net, memcache_connect() should return TRUE on success or FALSE on failure. Therefore, I thought the following code should work even if I change my cache server address to a non-existent address, but it didn't:

    $memcache=memcache_connect('myCacheServer.com', 11211);

    if($memcache){
        $this->connect=$memcache;
    }
    else{
        $memcache=memcache_connect('localhost', 11211);
        $this->connect=$memcache;
    }

Here's the error message I get:

Message: memcache_connect(): php_network_getaddresses: getaddrinfo failed: Temporary 
failure in name resolution

Does anyone know how else I can set up this simple boolean?

Upvotes: 1

Views: 6590

Answers (3)

I use this code for check connection

function checkConnection()
{
    try {
        $client = $this->initClient();
        $data = @$client->getVersion();
    } catch (Exception $e) {
        $data = false;
    }
    return !empty($data);
}

Upvotes: 0

tim peterson
tim peterson

Reputation: 24305

I found a solution from php.net's Memcache documentation that partially works. Meaning, the errors displayed to the user are suppressed but you still have to wait for a long timeout if the cache server doesn't exist.

Here's my code for this:

    $host='myCacheServer.com';
    $port=11211;
    $memcache = new Memcache();
    $memcache->addServer($host, $port);
    $stats = @$memcache->getExtendedStats();
    $available = (bool) $stats["$host:$port"];
    if ($available && @$memcache->connect($host, $port)){
            $this->connect=$memcache;
           // echo 'true';
    }

    else{
            $host='localhost';
            $memcache->addServer($host, $port);
            $this->connect=$memcache;
            //echo 'false';
    }    

Upvotes: 0

Robbie
Robbie

Reputation: 17710

As per comment, not sure why the above doesn't work, but there is a much better way of handling this.

If "myCacheServer.com" can't be connected to, then it may take up to 30 seconds each time to timeout. Then after the timeout, you'll fall back to local host - but not much point running memcached if you need to wait 30 seconds each time.

I'd recommend putting the server in a config file, or driving based off a known value - something like

if (isset($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'], 'localhost') ) !== false) {
    define('MEMCAHCED_SERVER', 'localhost');
    define('MEMCAHCED_PORT', '11211');
} else {
    // assume live - alwways have live as the fallback
    define('MEMCAHCED_SERVER', 'myCacheHost.com');
    define('MEMCAHCED_PORT', '11211');
}

$memcache=memcache_connect(MEMCAHCED_SERVER, MEMCAHCED_PORT);   

// Set the status to true or false.
$this->connect=$memcache;

Then, to meet your needs (if you expect the remote server to not be available) I would store this fact in a file on the server. It bit unsualy but will save you time.

// Before calling memcache connect
if (file_exists(MyFlagFile) and filemtime(MyFlagFile) > time() - 600) {
     // Do Not Use Memcached as it failed within hte last 5 minutes
} else {
     // Try to use memcached again

     if (!$memcache) {
         // Write a file to the server with the time, stopping more tries for the next 5 minutes
         file_put_contents(MyFlagFile, 'Failed again');
     }
 }

Upvotes: 1

Related Questions