PHP Couchbase/ext 1.0 Connecting to Multiple Servers in case of Failure
I am trying to implement a php couchbase/ext 1.0 connection similar to what is described here (and pasted in the code block below): http://www.couchbase.com/wiki/display/couchbase/Couchbase+PHP+Client+Lib...
However, I am having trouble programmatic determining if the new Couchbase object is connected. $cb comes back as an instance of Couchbase regardless if it connected to the server or not. The only differences I see when a connection fails (E.g. server1:8091 is down) I get a PHP warning that the connection failed ( Warning: Couchbase::__construct(): Failed to connect libcouchbase to server ) and the resource handle ( _handle:Couchbase:private ) is empty. I do not know what or how to accurately check for or catch the connection error.
I have read that the addServer method may be added and/or the connection method may be updated in version 1.1 but I am not ready to use etx 1.1 at this time.
Thank you for taking the time to offer any advice.
$servers = array("http://server1:8091", "http://server2:8091"); do { if(empty($servers)) { echo "no suitable server found"; // throw } $server = array_pop($servers); $cb = new Couchbase($server); } while(!$cb); // $cb is now usable.
Heya,
sorry for the confusion, I got the semantics for `new Couchbase()` wrong. To be able to loop through servers to find a healthy one with 1.0, you will need to use the procedural API:
function my_couchbase_connect($servers) {
do {
if(empty($servers)) {
echo "no suitable server found";
return false;
// throw
}
$server = array_pop($servers);
$cb = couchbase_connect($server);
} while(!$cb);
return $cb;
}
$servers = array("server1:8091", "server2:8091");
$cb = my_couchbase_connect($servers);
var_dump($cb);
I'll make sure to make this possible using the OO API as well.
Cheers
Jan
--
Thank you for the reply!
The connection while loop is now working with the procedural API. However, I need to update my app to manage the resource number. Do you know if there is any procedural documentation (E.g. couchbase_set, couchbase_get ) outside of what is available on github ( https://github.com/couchbaselabs/php-ext-couchbase/blob/master/couchbase.c )?
Any idea when the OO API maybe updated, couple days, few months?
Thank you again for the assistance.
Blake
On documentation, did you see http://www.couchbase.com/docs/couchbase-sdk-php-1.0/index.html ?
Also, it will probably be more in weeks/months for the OO API.
Thank you for the estimated time for the OO API update, I appreciate that.
In regards to the documentation, I have seen that manual. I am interested in something related to the procedural API. E.g. couchbase_connect(), couchbase_set(), couchbase_get(). I understand it may not be available at this time but any recommendation into the source or where else to look is appreciated.
I have an additional technical question, is it possible to pass a handle/resource to the Couchbase object? E.g. start a connection using couchbase_connect and passing its result to new Couchbase ?
Thank you again for your time,
Blake
Hi Jonny,
the procedural and OO APIs are mirrored, so couchbase_set() == Couchbase::set(), couchbase_get() == Couchbase::set() etc. the documentation and semantics are exactly the same, the only difference is that for the procedural API, the first argument to the functions is a valid Couchbase handle that you get from couchbase_connect() and for the OO API, you need an instance of the class Couchbase that you get with new Couchbase().
The API mirrors ext/memcached which is documented at http://php.net/memcached with the difference documented at http://www.couchbase.com/docs/couchbase-sdk-php-1.0/migrate-to-couchbase...
For the other question, you can't pass a couchbase_connect() handle to new Couchbase. We're not there yet, but the plan is that you can do everything with both APIs and you just pick based on the style you prefer.
I hope that helps :)
Cheers
Jan
--
Thank you very much Jan,
This will absolutely help me get going.
Hm, the trick must be in the while then. I think that was authored under the assumption that the $cb would evaluate to false if it was not connected. Let me get the author of that snippet to chime in.
We hope to have a new constructor option in the 1.0.x release timeframe as well too.