Skip to content

Commit 4493cdf

Browse files
committed
Added driverReadMultiple() support for Couchbasev3
1 parent 6b60a53 commit 4493cdf

File tree

5 files changed

+92
-10
lines changed

5 files changed

+92
-10
lines changed

lib/Phpfastcache/Core/Item/ExtendedCacheItemTrait.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,8 @@ public function setDriver(ExtendedCacheItemPoolInterface $driver): ExtendedCache
7777
*/
7878
public function getEncodedKey(): string
7979
{
80-
// Only calculate the encoded key on demand to save resources
8180
if (!isset($this->encodedKey)) {
82-
$keyHashFunction = $this->driver->getConfig()->getDefaultKeyHashFunction();
83-
84-
if ($keyHashFunction) {
85-
$this->encodedKey = $keyHashFunction($this->getKey());
86-
} else {
87-
$this->encodedKey = $this->getKey();
88-
}
81+
$this->encodedKey = $this->driver->getEncodedKey($this->getKey());
8982
}
9083

9184
return $this->encodedKey;

lib/Phpfastcache/Core/Pool/DriverBaseTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ public static function getItemClass(): string
142142
return self::$cacheItemClasses[static::class];
143143
}
144144

145+
/**
146+
* @inheritDoc
147+
*/
148+
public function getEncodedKey(string $key): string
149+
{
150+
$keyHashFunction = $this->getConfig()->getDefaultKeyHashFunction();
151+
152+
if ($keyHashFunction) {
153+
if (\is_callable($keyHashFunction)) {
154+
return $keyHashFunction($key);
155+
}
156+
throw new PhpfastcacheLogicException('Unable to build the encoded key (defaultKeyHashFunction is not callable)');
157+
}
158+
159+
return $key;
160+
}
161+
162+
145163

146164
/**
147165
* @param ExtendedCacheItemInterface $item

lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public static function getConfigClass(): string;
7777
*/
7878
public static function getItemClass(): string;
7979

80+
/**
81+
* @param string $key
82+
* @return string
83+
*/
84+
public function getEncodedKey(string $key): string;
85+
8086
/**
8187
* @return ConfigurationOptionInterface
8288
*/

lib/Phpfastcache/Drivers/Couchbasev3/Driver.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Couchbase\ClusterOptions;
2323
use Couchbase\Collection;
2424
use Couchbase\DocumentNotFoundException;
25+
use Couchbase\GetResult;
2526
use Couchbase\Scope;
2627
use Couchbase\UpsertOptions;
2728
use DateTimeInterface;
@@ -35,6 +36,8 @@
3536
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
3637
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
3738
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
39+
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedException;
40+
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedMethodException;
3841

3942
/**
4043
* @property Cluster $instance Instance of driver service
@@ -105,6 +108,33 @@ protected function driverRead(ExtendedCacheItemInterface $item): ?array
105108
}
106109
}
107110

111+
/**
112+
* @param ExtendedCacheItemInterface $item
113+
* @return ?array<string, mixed>
114+
*/
115+
protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array
116+
{
117+
try {
118+
$keys = $this->getKeys($items, true);
119+
$results = [];
120+
/**
121+
* CouchbaseBucket::get() returns a GetResult interface
122+
*/
123+
/** @var GetResult $document */
124+
foreach ($this->getCollection()->getMulti($this->getKeys($items, true)) as $document) {
125+
$content = $document->content();
126+
if ($content) {
127+
$decodedDocument = $this->decodeDocument($content);
128+
$results[$decodedDocument[ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX]] = $this->decodeDocument($content);
129+
}
130+
}
131+
132+
return $results;
133+
} catch (DocumentNotFoundException) {
134+
return [];
135+
}
136+
}
137+
108138
/**
109139
* @param ExtendedCacheItemInterface $item
110140
* @return bool
@@ -135,21 +165,47 @@ protected function driverDelete(string $key, string $encodedKey): bool
135165
{
136166

137167
try {
138-
$this->getCollection()->remove($encodedKey);
139-
return true;
168+
return $this->getCollection()->remove($encodedKey)->mutationToken() !== null;
140169
} catch (DocumentNotFoundException) {
141170
return true;
142171
} catch (CouchbaseException) {
143172
return false;
144173
}
145174
}
146175

176+
177+
/**
178+
* @param string[] $keys
179+
* @return bool
180+
*/
181+
protected function driverDeleteMultiple(array $keys): bool
182+
{
183+
try {
184+
$this->getCollection()->removeMulti(array_map(fn(string $key) => $this->getEncodedKey($key), $keys));
185+
return true;
186+
} catch (CouchbaseException) {
187+
return false;
188+
}
189+
}
190+
191+
147192
/**
148193
* @return bool
194+
* @throws PhpfastcacheUnsupportedMethodException
149195
*/
150196
protected function driverClear(): bool
151197
{
198+
if (!$this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->flushEnabled()) {
199+
$this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->enableFlush(true);
200+
if (!$this->instance->buckets()->getBucket($this->getConfig()->getBucketName())->flushEnabled()) {
201+
throw new PhpfastcacheUnsupportedMethodException(
202+
'Flushing operation is not enabled on your Bucket. See https://docs.couchbase.com/server/current/manage/manage-buckets/flush-bucket.html'
203+
);
204+
}
205+
}
206+
152207
$this->instance->buckets()->flush($this->getConfig()->getBucketName());
208+
153209
return true;
154210
}
155211

tests/lib/Helper/TestHelper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use League\CLImate\CLImate;
1919
use Phpfastcache\Api;
2020
use Phpfastcache\Config\ConfigurationOptionInterface;
21+
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
2122
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
2223
use Phpfastcache\Event\EventManagerInterface;
2324
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
@@ -543,12 +544,20 @@ public function runCRUDTests(ExtendedCacheItemPoolInterface|PhpfastcacheAbstract
543544
}
544545
unset($cacheItem);
545546

547+
$this->printInfoText('Testing deleting multiple keys at once.');
546548
$cacheItems = $pool->getItems([$cacheKey, $cacheKey2]);
547549
foreach ($cacheItems as $cacheItem) {
548550
$cacheItem->set(str_shuffle($cacheValue));
549551
$pool->save($cacheItem);
550552
}
551553
$pool->deleteItems(array_keys($cacheItems));
554+
555+
if(count(array_filter(array_map(fn(ExtendedCacheItemInterface $item) => $item->isHit(), $pool->getItems([$cacheKey, $cacheKey2])))) === 0) {
556+
$this->assertPass('The cache items does no longer exists in pool.');
557+
} else {
558+
$this->assertFail('The cache items still exists in pool.');
559+
return;
560+
}
552561
}
553562

554563
$this->printInfoText(

0 commit comments

Comments
 (0)