From e1fa8c1e2397808bc8bbc703aee5ffa478c35338 Mon Sep 17 00:00:00 2001 From: Mirko Bukilic Date: Fri, 12 Apr 2024 13:00:24 +0200 Subject: [PATCH] Updated Couchbase4.x for data serialization --- .../Mcache/Driver/Couchbase/Couchbase4x.php | 15 ++++++-- src/G4/Mcache/SerializeTranscoder.php | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/G4/Mcache/SerializeTranscoder.php diff --git a/src/G4/Mcache/Driver/Couchbase/Couchbase4x.php b/src/G4/Mcache/Driver/Couchbase/Couchbase4x.php index 4014fa2..6099728 100644 --- a/src/G4/Mcache/Driver/Couchbase/Couchbase4x.php +++ b/src/G4/Mcache/Driver/Couchbase/Couchbase4x.php @@ -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 { @@ -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; } @@ -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; } @@ -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(); @@ -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(); diff --git a/src/G4/Mcache/SerializeTranscoder.php b/src/G4/Mcache/SerializeTranscoder.php new file mode 100644 index 0000000..6ec4b34 --- /dev/null +++ b/src/G4/Mcache/SerializeTranscoder.php @@ -0,0 +1,37 @@ +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; + } +}