/** * Get a Document by it's key. * * @param string $key * * @return Document * * @SuppressWarnings(PHPMD.StaticAccess) */ public function get($key) { $rawDocument = $this->connection->get($key); if (!$rawDocument) { return null; } return new Document($key, $rawDocument); }
/** * Returns cached value by key or false if there is no cache entry for the given key * * @param string $key * @return bool|mixed */ public function get($key) { try { return $this->cache->get($this->prepareKey($key)); } catch (\CouchbaseException $e) { return false; } }
/** * {@inheritDoc} */ public function find($storageName, $key) { $value = $this->client->get($key); if ($value === null) { throw new NotFoundException(); } return $value; }
protected function getKeysOfTag($tag) { $data = $this->Couchbase->get($this->tag_prefix . $tag); if (empty($data)) { return false; } $keys = json_decode('[' . ltrim($data, ',') . ']'); return $keys; }
<?php $cb = new Couchbase("ec2-50-112-3-209.us-west-2.compute.amazonaws.com:8091"); // uses the default bucket // or specify a specific bucket like so: //$cb = new Couchbase("127.0.0.1:8091", "bucket", "pass", "bucket"); $cb->set("rami", 1); var_dump($cb->get("rami"));
print "GET : " . $timer->getTime() . "\n"; exit; // ---------- Couchbase print Ansi::bold("Couchbase\n"); $timer->start(); for ($i = 0; $i < $nbrLoops; $i++) { foreach ($dataList as $key => $data) { $couch->set("{$key}-{$i}", $data); } } $timer->stop(); print "PUT : " . $timer->getTime() . "\n"; $timer->start(); for ($i = 0; $i < $nbrLoops; $i++) { foreach ($dataList as $key => $data) { $couch->get("{$key}-{$i}"); } } $timer->stop(); print "GET : " . $timer->getTime() . "\n"; exit; // ---------- Redis print Ansi::bold("Redis\n"); $timer->start(); for ($i = 0; $i < $nbrLoops; $i++) { foreach ($dataList as $key => $data) { $ndb->set("{$key}-{$i}", $data); } } $timer->stop(); print "PUT : " . $timer->getTime() . "\n";
<?php echo "--------------------------------------------------------------------------\n"; echo "\tCouchbase Storage Operations\n"; echo "--------------------------------------------------------------------------\n"; $cb = new Couchbase(); // Create a key $cb->set("mytest", 1); echo "\n--------------\n"; echo "Get mytest: "; echo $cb->get("mytest"); // try to add a key that exists $result = $cb->add("mytest", 2); if ($result) { echo "\n--------------\n"; echo "Add succeeded"; } else { echo "\n--------------\n"; echo "Add failed: key exists"; } // success $result = $cb->replace("mytest", 2); echo "\n--------------\n"; echo "Get mytest: "; echo $cb->get("mytest"); // try to replace a key that does not exist $result = $cb->add("mytest4", 2); if ($result) { echo "\n--------------\n"; echo "Replace succeeded"; } else {
<?php echo "--------------------------------------------------------------------------\n"; echo "\tCouchbase Non-JSON Ops\n"; echo "--------------------------------------------------------------------------\n"; if (file_exists("./output.jpg")) { unlink("./output.jpg"); } $cb = new Couchbase(); echo "Store Strings, Floats, Integers\n"; $cb->set("store_strings", "The quick brown fox jumped over the lazy dog.", 10); $cb->set("store_floats", 3.14159265358979, 10); $cb->set("store_integers", -42, 10); echo "get 'store_strings => '"; echo $cb->get("store_strings"); echo "\n\n"; echo "get 'store_floats' => "; echo $cb->get("store_floats"); echo "\n\n"; echo "get 'store_integers' => "; echo $cb->get("store_integers"); echo "\n\n"; echo "--------------------------------------------------------------------------\n"; echo "Append/Prepend Ops\n"; echo "Create a list, start with one item\n"; $cb->set("mylist", "oranges", 30); echo "Add an item...\n\n"; $cb->prepend("mylist", "apples,"); echo "Add an item...\n\n"; $cb->append("mylist", ",bananas"); echo "Add an item...\n\n";
/** * Partial Update: Update/Add individual fields in a JSON Document safely * Complete Update: Create/Replace the entire Document, * the given object is encoded to JSON * @param $key Document Identifier * @param $obj Either Entire Document object for complete update OR * An array with partial updates as keys where * key is an index for an array otherwise the field name. * Each key is associated with path and val properties. * Path is an array of the field path from the root (doc's parent object) and * Val is the new value of the field. * @param $is_partial Boolean to identify a partial update * @params $cas, $persist_to, $replicate_to function similar to SET operation * @return Boolean true on SUCCESS else false */ public function update($key, $obj, $is_partial = false, $expiry = 0, $persist_to = 0, $replicate_to = 0) { $counter = 1; $n_json = NULL; do { $v_json = parent::get($key, NULL, $cas); if ($obj) { if ($is_partial == true) { if (!$cas) { throw new Exception("No existing document for partial update"); } $valGet = json_decode($v_json); $val = self::_merge($valGet, $obj); $n_json = json_encode($val); } else { $cas = 0; $n_json = json_encode($obj); } } $rv = parent::set($key, $n_json, $expiry, $cas, $persist_to, $replicate_to); } while ($rv == false && ++$counter <= $this->max_retries); return $rv; }
<?php session_start(); if (!isset($_SESSION["email"])) { header("location: index.php"); } $cb = new Couchbase("http://counterstrikevm.cloudapp.net:8091/", "", "", "default"); $email = $_SESSION["email"]; $password = $cb->get($email); ?> <!doctype html> <html> <head> <title>Node Authentication</title> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> <style> body { padding-top:80px; word-wrap:break-word; } </style> </head> <body> <div class="container"> <div class="page-header text-center"> <h1><span class="fa fa-anchor"></span> Profile Page</h1> <a href="logout.php?logout">Sign Out</a> </div> <div class="row"> <!-- LOCAL INFORMATION -->
<?php echo "--------------------------------------------------------------------------\n"; echo "\t Couchbase Atomic Counters\n"; echo "--------------------------------------------------------------------------\n"; $cb = new Couchbase(); echo "Set Counter to 0\n"; $cb->set("counter", 0); echo "Get counter: "; echo $cb->get("counter"); echo "\n"; $cb->increment("counter"); echo "Get counter: "; echo $cb->get("counter"); echo "\n"; echo "Increment by 10\n"; $cb->increment("counter", 10); echo "Get counter: "; echo $cb->get("counter"); echo "\n"; echo "\nDelete the counter\n"; $cb->delete("counter"); echo "\nInit the value to 10\n"; // create an set an initial $cb->increment("counter", 1, true, 0, 10); echo "Get counter: "; echo $cb->get("counter"); echo "\n"; // does not decrease below 0 $cb->set("counter", 0); $cb->decrement("counter");
$user_data1->name = "John Smith"; $user_data1->email = "*****@*****.**"; $user_data1->password = "******"; $user_data1->logins = 0; $user_data2 = new Person(); $user_data2->doctype = "learn"; $user_data2->username = "******"; $user_data2->name = "Xavier Doe"; $user_data2->email = "*****@*****.**"; $user_data2->password = "******"; $user_data2->logins = 0; // initialize the documents $cb->set($user_data1->email, json_encode($user_data1)); $cb->set($user_data2->email, json_encode($user_data2)); // retrieve the document and output $doc = $cb->get($user_data1->email); echo "\n--------------\nRetrieve Doc\n"; echo $doc; echo "\n--------------\n"; var_dump(Person::fromJSON($doc)); $first_cas = null; $doc = json_decode($cb->get($user_data1->email, null, $first_cas)); echo "\n--------------\nRetrieve Doc and get CAS\n"; echo "1st CAS: {$first_cas}"; echo "\n--------------\n"; echo "update doc and look at cas"; $doc->logins += 1; echo "\n--------------\n"; echo "Logins: {$doc->logins} \n"; $cb->replace($user_data1->email, json_encode($user_data1)); $second_cas = null;
public function read($id) { $sId = $this->getSid($id); $result = $this->_cb->get($sId); return $result; }
<?php echo "--------------------------------------------------------------------------\n"; echo "\tCouchbase Views\n"; echo "--------------------------------------------------------------------------\n"; // Create the view as documented here: // http://www.couchbase.com/docs/couchbase-sdk-php-1.1/tutorial-preparations-cb-server.html $cb = new Couchbase("127.0.0.1:8091", "", "", "beer-sample"); echo "\n--------------------------------------------------------------------------\n"; $views = $cb->getDesignDoc('brewery'); var_export(json_decode($views)); echo "\n--------------------------------------------------------------------------\n"; echo "Breweries (by_name)\n"; $results = $cb->view("brewery", "by_name", array('limit' => 10)); foreach ($results['rows'] as $row) { $doc = $cb->get($row['id']); if ($doc) { $doc = json_decode($doc, true); echo "\nName : "; echo $doc["name"]; } } echo "\n--------------------------------------------------------------------------\n"; echo "Breweries (by_name), and output country\n"; $results = $cb->view("brewery", "by_name", array('limit' => 10)); foreach ($results['rows'] as $row) { $doc = $cb->get($row['id']); if ($doc) { $doc = json_decode($doc, true); echo "\nname : "; echo $doc["name"];
<?php $cb = new Couchbase("http://counterstrikevm.cloudapp.net:8091/", "", "", "default"); $email = $_POST["email"]; $password = $_POST["password"]; $password2 = $_POST["confirm"]; //Sanitize DATA HERE if ($password == $_POST["confirm"]) { $user = $cb->get($email); if ($user == false) { $cb->set($email, $password); echo "<SCRIPT LANGUAGE='JavaScript'>\n\t\twindow.alert('Registered!')\n\t\twindow.location.href='http://localhost/';\n\t\t</SCRIPT>"; } else { echo "<SCRIPT LANGUAGE='JavaScript'>\n\t\twindow.alert('User already exist!')\n\t\twindow.location.href='http://localhost/';\n\t\t</SCRIPT>"; } } else { echo "<SCRIPT LANGUAGE='JavaScript'>\n window.alert('Password does not match!')\n window.location.href='http://localhost/';\n </SCRIPT>"; }
echo "\tCouchbase - Compare and Swap\n"; echo "--------------------------------------------------------------------------\n"; $cb = new Couchbase(); $user_data1 = new stdClass(); $user_data1->doctype = "learn"; $user_data1->username = "******"; $user_data1->name = "John Smith"; $user_data1->email = "*****@*****.**"; $user_data1->password = "******"; $user_data1->logins = 0; echo "--------------------------------------------------------------------------\n"; echo "Set the Data, then Retrieve Two Copies of the Document\n"; // initialize the documents $cb->set($user_data1->email, json_encode($user_data1)); // retrieve the document and output $v1 = json_decode($cb->get($user_data1->email, null, $cas)); echo "--------------------------------------------------------------------------\n"; echo "CAS : {$cas}\n"; $cas1 = $cas; $v2 = json_decode($cb->get($user_data1->email, null, $cas)); $cas2 = $cas; echo "--------------------------------------------------------------------------\n"; echo "Both v1 and v2 are identical and have identical CAS [(cas1) {$cas1} == (cas2) {$cas2}]\n"; echo "\n--------------\n"; echo "Now We'll update the document which results in a new CAS"; $v1->logins += 1; $cas1 = $cb->set($v1->email, json_encode($v1)); echo "Now CAS as changed [(cas1) {$cas1} != (cas2) {$cas2}]"; echo "\n\n--------------\n"; $result = $cb->replace($v2->email, json_encode($v2), 0, $cas2); if ($result) {