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
15 changes: 11 additions & 4 deletions src/G4/Mcache/Driver/Couchbase/Couchbase4x.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace G4\Mcache\Driver\Couchbase;

use Couchbase\ClusterOptions;
use Couchbase\GetOptions;
use Couchbase\Cluster;
use Couchbase\UpsertOptions;
use Couchbase\ReplaceOptions;
use G4\Mcache\SerializeTranscoder;

class Couchbase4x implements CouchbaseInterface
{
Expand All @@ -32,8 +34,7 @@ public function delete($key)
return false;
}
try {
$mutationResult = $this->clientFactory()->defaultCollection()->remove($key);
return $mutationResult->cas();
return $this->clientFactory()->defaultCollection()->remove($key)->cas();
} catch (\Exception $e) {
return false;
}
Expand All @@ -44,8 +45,11 @@ public function get($key)
if (!$this->clientFactory()) {
return false;
}

$options = new GetOptions();
$options->transcoder(new SerializeTranscoder());
try {
return $this->clientFactory()->defaultCollection()->get($key)->content();
return $this->clientFactory()->defaultCollection()->get($key, $options)->content();
} catch (\Exception $e) {
return false;
}
Expand All @@ -58,6 +62,7 @@ public function replace($key, $value, $expiration)
}
$replaceOptions = (new ReplaceOptions())
->expiry($expiration);
$replaceOptions->transcoder(new SerializeTranscoder());
try {
$mutationResult = $this->clientFactory()->defaultCollection()->replace($key, $value, $replaceOptions);
return $mutationResult->cas();
Expand All @@ -71,8 +76,10 @@ public function set($key, $value, $expiration)
if (!$this->clientFactory()) {
return false;
}

$upsertOptions = (new UpsertOptions())
->expiry($expiration);
->expiry($expiration)
->transcoder(new SerializeTranscoder());
try {
$mutationResult = $this->clientFactory()->defaultCollection()->upsert($key, $value, $upsertOptions);
return $mutationResult->cas();
Expand Down
37 changes: 37 additions & 0 deletions src/G4/Mcache/SerializeTranscoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace G4\Mcache;

use Couchbase\Exception\DecodingFailureException;
use Couchbase\Transcoder;
use Couchbase\TranscoderFlags;

class SerializeTranscoder implements Transcoder
{
private static ?SerializeTranscoder $instance;

public static function getInstance(): Transcoder
{
if (!isset(self::$instance)) {
self::$instance = new SerializeTranscoder();
}
return self::$instance;
}

public function encode($value): array
{
return [
serialize($value),
(new TranscoderFlags(TranscoderFlags::DATA_FORMAT_BINARY))->encode(),
];
}

public function decode(string $bytes, int $flags = 0)
{
$data = unserialize($bytes);
if ($data === false) {
throw new DecodingFailureException('Unable to unserialize bytes with SerializeTranscoder');
}
return $data;
}
}