Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ vendor
tests/unit/coverage/
tests/unit/fixtures/*
!tests/unit/fixtures/.gitkeep
tests/unit/.phpunit.cache
tests/unit/.phpunit.result.cache
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@
"psr-0": {"G4\\Mcache\\": "src/"}
},
"require-dev": {
"phpunit/phpunit": "5.7.*"
"phpunit/phpunit": "10.*"
},
"require": {
"php" : ">=5.6",
"php" : ">=8.2",
"g4/profiler" : ">=1.10.0",
"g4/value-object" : "*"
"g4/value-object" : "*",
"couchbase/couchbase": "*"
},
"suggest": {
"ext-couchbase": "^4.0"
},
"scripts": {
"unit-test": "./vendor/bin/phpunit --configuration tests/unit/phpunit.xml"
}
}
34 changes: 34 additions & 0 deletions playground/couchbase4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

// NOTE: Change the below vendor path to your own.
require_once __DIR__ . '/../vendor/autoload.php';

use Couchbase\ClusterOptions;
use Couchbase\Cluster;

// Update these credentials for your Local instance!
$connectionString = "couchbase://localhost";
$options = new ClusterOptions();

$options->credentials("Administrator", "password");
$cluster = new Cluster($connectionString, $options);

// get a bucket reference
$bucket = $cluster->bucket("travel-sample");

// get a user-defined collection reference
$scope = $bucket->scope("tenant_agent_00");
$collection = $scope->collection("users");

$upsertResult = $collection->upsert("my-document-key", ["name" => "Ted", "Age" => 31]);

$getResult = $collection->get("my-document-key");

print_r($getResult->content());

$queryResult = $cluster->query("select \"Hello World\" as greeting");

// Iterate over the rows to access result data and print to the terminal.
foreach ($queryResult->rows() as $row) {
printf("%s\n", $row["greeting"]);
}
44 changes: 34 additions & 10 deletions src/G4/Mcache/Driver/Couchbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use G4\Mcache\Driver\Couchbase\Couchbase1x;
use G4\Mcache\Driver\Couchbase\Couchbase2x;
use G4\Mcache\Driver\Couchbase\Couchbase4x;
use G4\Mcache\Driver\Couchbase\CouchbaseInterface;

class Couchbase extends DriverAbstract
{

const DEFAULT_TIMEOUT_VALUE = 2500000; // time in microseconds
private const DEFAULT_TIMEOUT_VALUE = 500000; // time in microseconds

/**
* @var array
Expand All @@ -26,10 +27,19 @@ class Couchbase extends DriverAbstract
*/
private $user;

/**
* @var string
*/
private $pass;

/**
* @var bool
*/
private $persistent;

/**
* @var int
*/
private $timeout;

/**
Expand Down Expand Up @@ -58,10 +68,10 @@ private function processOptions()
$this->servers[] = $server;
}
$this->bucket = $options['bucket'];
$this->user = isset($options['user']) ? $options['user'] : '';
$this->pass = isset($options['pass']) ? $options['pass'] : '';
$this->persistent = isset($options['persistent']) ? (bool) $options['persistent'] : false;
$this->timeout = isset($options['timeout']) ? (int) $options['timeout'] : self::DEFAULT_TIMEOUT_VALUE;
$this->user = $options['user'] ?? '';
$this->pass = $options['pass'] ?? '';
$this->persistent = isset($options['persistent']) && (bool)$options['persistent'];
$this->timeout = isset($options['timeout']) ? (int) $options['timeout'] : self::DEFAULT_TIMEOUT_VALUE;

return $this;
}
Expand Down Expand Up @@ -103,7 +113,9 @@ private function connect()
$this->persistent,
$this->timeout
);
} else if (class_exists('\CouchbaseCluster')) {
return $this->driver;
}
if (class_exists('\CouchbaseCluster')) {
$this->driver = new Couchbase2x(
$this->servers,
$this->user,
Expand All @@ -112,9 +124,21 @@ private function connect()
$this->persistent,
$this->timeout
);
} else {
throw new \Exception('Couchbase client missing!', 601);
return $this->driver;
}
return $this->driver;

if (class_exists('\Couchbase\Cluster')) {
$this->driver = new Couchbase4x(
$this->servers,
$this->user,
$this->pass,
$this->bucket,
$this->persistent,
$this->timeout
);
return $this->driver;
}

throw new \Exception('Couchbase client missing!', 601);
}
}
}
108 changes: 108 additions & 0 deletions src/G4/Mcache/Driver/Couchbase/Couchbase4x.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace G4\Mcache\Driver\Couchbase;

use Couchbase\ClusterOptions;
use Couchbase\Cluster;
use Couchbase\UpsertOptions;
use Couchbase\ReplaceOptions;

class Couchbase4x implements CouchbaseInterface
{
private const DEFAULT_CONNECT_TIMEOUT = 500000;
private ?\Couchbase\Cluster $clientCluster;
private ?\Couchbase\Bucket $clientBucket;

public function __construct(
private readonly array $servers,
private readonly string $user,
private readonly string $pass,
private readonly string $bucket,
private readonly bool $persistent,
private int $timeout
) {
$this->clientCluster = null;
$this->clientBucket = null;
$this->timeout = $timeout ?? self::DEFAULT_CONNECT_TIMEOUT;
}

public function delete($key)
{
if (!$this->clientFactory()) {
return false;
}
try {
$mutationResult = $this->clientFactory()->defaultCollection()->remove($key);
return $mutationResult->cas();
} catch (\Exception $e) {
return false;
}
}

public function get($key)
{
if (!$this->clientFactory()) {
return false;
}
try {
return $this->clientFactory()->defaultCollection()->get($key)->content();
} catch (\Exception $e) {
return false;
}
}

public function replace($key, $value, $expiration)
{
if (!$this->clientFactory()) {
return false;
}
$replaceOptions = (new ReplaceOptions())
->expiry($expiration);
try {
$mutationResult = $this->clientFactory()->defaultCollection()->replace($key, $value, $replaceOptions);
return $mutationResult->cas();
} catch (\Exception $e) {
return false;
}
}

public function set($key, $value, $expiration)
{
if (!$this->clientFactory()) {
return false;
}
$upsertOptions = (new UpsertOptions())
->expiry($expiration);
try {
$mutationResult = $this->clientFactory()->defaultCollection()->upsert($key, $value, $upsertOptions);
return $mutationResult->cas();
} catch (\Exception $e) {
return false;
}
}

private function clientFactory()
{
if ($this->clientBucket instanceof \Couchbase\Bucket) {
return $this->clientBucket;
}

$this->connect();
return $this->clientBucket;
}

private function connect()
{
$connectionString = 'couchbase://' . implode(',', $this->servers);
$options = new ClusterOptions();
$options->connectTimeout($this->timeout);
$options->credentials($this->user, $this->pass);
try {
$this->clientCluster = new Cluster($connectionString, $options);
$this->clientBucket = $this->clientCluster->bucket($this->bucket);
} catch (\Exception $e) {
$message = sprintf('Could not connect to couchbase cluster %s, bucket %s, message: %s', $connectionString, $this->bucket, $e->getMessage());
trigger_error($message, E_USER_WARNING);
}
}
}
6 changes: 5 additions & 1 deletion src/G4/Mcache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ public function set($data)

private function getFromCache()
{
return json_decode($this->mcache->get(), true);
$value = $this->mcache->get();
if (!$value) {
return null;
}
return json_decode($value, true);
}

private function setToCache()
Expand Down
29 changes: 16 additions & 13 deletions tests/unit/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<phpunit bootstrap="bootstrap.php" colors="true">

<testsuite name="Test Suite">
<directory>./src</directory>
</testsuite>

<filter>
<whitelist>
<directory suffix=".php">../../src/</directory>
</whitelist>
</filter>

</phpunit>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
>
<testsuite name="Test Suite">
<directory>./src</directory>
</testsuite>
<source>
<include>
<directory suffix=".php">../../src/</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions tests/unit/src/G4/Mcache/Driver/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

use G4\Mcache\Driver\File;

class FileTest extends PHPUnit_Framework_TestCase
class FileTest extends \PHPUnit\Framework\TestCase
{

public function testInstance()
{

$this->markTestSkipped('Skipping testInstance');
}
}
16 changes: 6 additions & 10 deletions tests/unit/src/G4/Mcache/Driver/LibmemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@

use G4\Mcache\Mcache;

class LibmemcachedTest extends \PHPUnit_Framework_TestCase
class LibmemcachedTest extends \PHPUnit\Framework\TestCase
{

private $_driver;
private $driver;


public function setUp()
public function setUp(): void
{
$this->_driver = new \G4\Mcache\Driver\Libmemcached();

parent::setUp();
$this->driver = new \G4\Mcache\Driver\Libmemcached();
}


public function tearDown()
public function tearDown(): void
{
unset(
$this->_driver
$this->driver
);

parent::tearDown();
}

public function testGet()
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/src/G4/Mcache/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
use G4\ValueObject\StringLiteral;
use G4\ValueObject\Pathname;

class FileCacheTest extends \PHPUnit_Framework_TestCase
class FileCacheTest extends \PHPUnit\Framework\TestCase
{

private $pathname;
private $key;

protected function setUp()
protected function setUp(): void
{
$this->pathname = new Pathname('');
$this->key = new StringLiteral('KEY');
}

protected function tearDown()
protected function tearDown(): void
{
$this->pathname = null;
$this->key = null;
Expand Down
Loading