示例#1
0
 /**
  * Caches value by key
  *
  * @param string $key
  * @param mixed $value
  * @param int $expireIn Seconds
  * @return bool
  */
 public function set($key, $value, $expireIn = 0)
 {
     try {
         return (bool) $this->cache->set($this->prepareKey($key), $value, $expireIn);
     } catch (\CouchbaseException $e) {
         return false;
     }
 }
 /**
  * @param string $sessionId
  * @param string $sessionData
  *
  * @return bool
  */
 public function write($sessionId, $sessionData)
 {
     $key = $this->keyPrefix . $sessionId;
     if (strlen($sessionData) < 1) {
         return false;
     }
     $startTime = microtime(true);
     $result = $this->connection->set($key, json_encode($sessionData), $this->lifetime);
     $this->newRelicApi->addCustomMetric(self::METRIC_SESSION_WRITE_TIME, microtime(true) - $startTime);
     return $result ? true : false;
 }
示例#3
0
 /**
  * Utility function that waits for bucket creation.
  * Bucket creation is async, for the time being, we need to poll until
  * it is there.
  * @param Couchbase $cb Couchbase client library object.
  * @param constant Expected memcached result code.
  */
 function _waitForBucket($cb, $resultCode = Memcached::RES_SUCCESS)
 {
     // var_dump("--waitForBucket");
     do {
         $cb->set("f", 1);
         usleep(500000);
         // 1/2 second
         // var_dump($cb->getResultMessage());
     } while ($cb->getResultCode() !== $resultCode);
     $cb->delete("f");
     // var_dump("--done waiting");
 }
示例#4
0
 /**
  * @param array $table
  * @return void
  */
 protected function save_TTL_table($table)
 {
     if (!empty($table)) {
         $t = time();
         foreach ($table as $key => $ttl) {
             if ($ttl !== 0 && $ttl < $t) {
                 unset($table[$key]);
             }
         }
     }
     try {
         $this->Couchbase->set($this->ttl_table_name, $table, 0);
     } catch (\Exception $Exception) {
         $this->ReportError('Couchbase can not save ttl table', __LINE__);
     }
 }
示例#5
0
$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;
$key = $user_data1->email;
$document = json_encode($user_data1);
//Store a key, but don't return success until it is written
//to disk on the master (or times out)
echo "\n--------------\n";
echo "Set Document with Observe Asynchronously, Returns when Persisted, and output return values\n";
try {
    $status = $cb->set($key, $document, 0, $cas = "", $persist_to = 1);
    echo "status/cas: {$status}";
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "";
}
//Store a key, but don't return success until it is
//replicated to 2 nodes (or times out)
//will fail on a single or two node cluster
echo "\n--------------\n";
try {
    $status = $cb->set($key, $document, 0, $cas = "", $replicate_to = 2);
    echo $status;
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "";
}
//Store a key, but don't return success until it is written
示例#6
0
 private function createCouchbaseDB(\Couchbase $db)
 {
     $db->set('oauth_clients-oauth_test_client', json_encode(array('client_id' => "oauth_test_client", 'client_secret' => "testpass", 'redirect_uri' => "http://example.com", 'grant_types' => 'implicit password')));
     $db->set('oauth_access_tokens-testtoken', json_encode(array('access_token' => "testtoken", 'client_id' => "Some Client")));
     $db->set('oauth_authorization_codes-testcode', json_encode(array('access_token' => "testcode", 'client_id' => "Some Client")));
     $db->set('oauth_users-testuser', json_encode(array('username' => "testuser", 'password' => "password")));
     $db->set('oauth_jwt-oauth_test_client', json_encode(array('client_id' => 'oauth_test_client', 'key' => $this->getTestPublicKey(), 'subject' => 'test_subject')));
 }
示例#7
0
<?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

echo "--------------------------------------------------------------------------\n";
echo "\tCouchbase JSON Doc Storage\n";
echo "--------------------------------------------------------------------------\n";
$cb = new Couchbase();
$mydoc = array("doctype" => "test", "name" => "John Smith");
echo "Original Document:\n";
var_dump($mydoc);
echo "\nSet Document (Hash -> JSON)\n";
// store a json doc (encode it)
$cb->set("mydoc", json_encode($mydoc));
echo "Retrieve Document (JSON -> Hash), also set a 10 second TTL\n";
// retrieve and decode json doc
$doc = $cb->getAndTouch("mydoc", 10);
echo "\n--------------\n";
echo "JSON String: " . $doc;
echo "\n--------------\n";
var_dump(json_decode($doc, true));
// (as array, not object)
echo "--------------------------------------------------------------------------\n";
示例#11
0
$user_data1 = new Person();
$user_data1->doctype = "learn";
$user_data1->username = "******";
$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";
示例#12
0
 public function write($id, $data)
 {
     $sId = $this->getSid($id);
     $result = $this->_cb->set($sId, $data, $this->_lifetime);
     return $result;
 }
示例#13
0
 /**
  * Push a Document to Couchbase.
  *
  * @param Document $document
  */
 public function set(Document $document)
 {
     $this->connection->set($document->getKey(), $document->getValue());
 }
示例#14
0
<?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>";
}
示例#15
0
$timer->start();
for ($i = 0; $i < $nbrLoops; $i++) {
    foreach ($dataList as $key => $data) {
        $sql = "SELECT data\n\t\t\tFROM Data\n\t\t\tWHERE name = '{$key}-{$i}'";
        $result = $db->queryOne($sql);
    }
}
$timer->stop();
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");
<?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");
示例#17
0
<?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"));
示例#18
0
echo "--------------------------------------------------------------------------\n";
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";