Skip to content

Commit 3338c7d

Browse files
authored
Merge pull request #10 from mirko-bukilic/master
Updated Couchbase4.x for data serialization
2 parents 05a1ed4 + e1fa8c1 commit 3338c7d

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

src/G4/Mcache/Driver/Couchbase/Couchbase4x.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace G4\Mcache\Driver\Couchbase;
44

55
use Couchbase\ClusterOptions;
6+
use Couchbase\GetOptions;
67
use Couchbase\Cluster;
78
use Couchbase\UpsertOptions;
89
use Couchbase\ReplaceOptions;
10+
use G4\Mcache\SerializeTranscoder;
911

1012
class Couchbase4x implements CouchbaseInterface
1113
{
@@ -32,8 +34,7 @@ public function delete($key)
3234
return false;
3335
}
3436
try {
35-
$mutationResult = $this->clientFactory()->defaultCollection()->remove($key);
36-
return $mutationResult->cas();
37+
return $this->clientFactory()->defaultCollection()->remove($key)->cas();
3738
} catch (\Exception $e) {
3839
return false;
3940
}
@@ -44,8 +45,11 @@ public function get($key)
4445
if (!$this->clientFactory()) {
4546
return false;
4647
}
48+
49+
$options = new GetOptions();
50+
$options->transcoder(new SerializeTranscoder());
4751
try {
48-
return $this->clientFactory()->defaultCollection()->get($key)->content();
52+
return $this->clientFactory()->defaultCollection()->get($key, $options)->content();
4953
} catch (\Exception $e) {
5054
return false;
5155
}
@@ -58,6 +62,7 @@ public function replace($key, $value, $expiration)
5862
}
5963
$replaceOptions = (new ReplaceOptions())
6064
->expiry($expiration);
65+
$replaceOptions->transcoder(new SerializeTranscoder());
6166
try {
6267
$mutationResult = $this->clientFactory()->defaultCollection()->replace($key, $value, $replaceOptions);
6368
return $mutationResult->cas();
@@ -71,8 +76,10 @@ public function set($key, $value, $expiration)
7176
if (!$this->clientFactory()) {
7277
return false;
7378
}
79+
7480
$upsertOptions = (new UpsertOptions())
75-
->expiry($expiration);
81+
->expiry($expiration)
82+
->transcoder(new SerializeTranscoder());
7683
try {
7784
$mutationResult = $this->clientFactory()->defaultCollection()->upsert($key, $value, $upsertOptions);
7885
return $mutationResult->cas();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace G4\Mcache;
4+
5+
use Couchbase\Exception\DecodingFailureException;
6+
use Couchbase\Transcoder;
7+
use Couchbase\TranscoderFlags;
8+
9+
class SerializeTranscoder implements Transcoder
10+
{
11+
private static ?SerializeTranscoder $instance;
12+
13+
public static function getInstance(): Transcoder
14+
{
15+
if (!isset(self::$instance)) {
16+
self::$instance = new SerializeTranscoder();
17+
}
18+
return self::$instance;
19+
}
20+
21+
public function encode($value): array
22+
{
23+
return [
24+
serialize($value),
25+
(new TranscoderFlags(TranscoderFlags::DATA_FORMAT_BINARY))->encode(),
26+
];
27+
}
28+
29+
public function decode(string $bytes, int $flags = 0)
30+
{
31+
$data = unserialize($bytes);
32+
if ($data === false) {
33+
throw new DecodingFailureException('Unable to unserialize bytes with SerializeTranscoder');
34+
}
35+
return $data;
36+
}
37+
}

0 commit comments

Comments
 (0)