From 3d0b22f52fdda8345a2e9f50fa7f48a68bf08880 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 13:21:32 +0100
Subject: [PATCH 01/11] feat: Add BulkWrite interface and Memcached
implementation
---
src/Caching/BulkWriter.php | 29 +++++++++++++++
src/Caching/Storages/MemcachedStorage.php | 44 +++++++++++++++++++++--
2 files changed, 71 insertions(+), 2 deletions(-)
create mode 100644 src/Caching/BulkWriter.php
diff --git a/src/Caching/BulkWriter.php b/src/Caching/BulkWriter.php
new file mode 100644
index 0000000..d9f9a8a
--- /dev/null
+++ b/src/Caching/BulkWriter.php
@@ -0,0 +1,29 @@
+Similar to write()
, but instead of a single key/value item, it works on multiple items specified in items
+ *
+ * @param array{string, mixed} $items An array of key/data pairs to store on the server
+ * @param array $dp Global dependencies of each stored value
+ * @return bool Returns true
on success or false
on failure
+ * @throws Nette\NotSupportedException
+ * @throws Nette\InvalidStateException
+ */
+ function bulkWrite(array $items, array $dp): bool;
+}
diff --git a/src/Caching/Storages/MemcachedStorage.php b/src/Caching/Storages/MemcachedStorage.php
index 03af23b..c4bf569 100644
--- a/src/Caching/Storages/MemcachedStorage.php
+++ b/src/Caching/Storages/MemcachedStorage.php
@@ -16,7 +16,7 @@
/**
* Memcached storage using memcached extension.
*/
-class MemcachedStorage implements Nette\Caching\Storage, Nette\Caching\BulkReader
+class MemcachedStorage implements Nette\Caching\Storage, Nette\Caching\BulkReader, Nette\Caching\BulkWriter
{
/** @internal cache structure */
private const
@@ -167,8 +167,48 @@ public function write(string $key, $data, array $dp): void
$this->memcached->set($key, $meta, $expire);
}
+ public function bulkWrite(array $items, array $dp): bool
+ {
+ if (isset($dp[Cache::Items])) {
+ throw new Nette\NotSupportedException('Dependent items are not supported by MemcachedStorage.');
+ }
- public function remove(string $key): void
+ $records = [];
+
+ $expire = 0;
+ if (isset($dp[Cache::Expire])) {
+ $expire = (int) $dp[Cache::Expire];
+ }
+
+ foreach ($items as $key => $data) {
+ $key = urlencode($this->prefix . $key);
+ $meta = [
+ self::MetaData => $data,
+ ];
+
+ if (!empty($dp[Cache::Sliding])) {
+ $meta[self::MetaDelta] = $expire; // sliding time
+ }
+
+ if (isset($dp[Cache::Callbacks])) {
+ $meta[self::MetaCallbacks] = $dp[Cache::Callbacks];
+ }
+
+ if (isset($dp[Cache::Tags]) || isset($dp[Cache::Priority])) {
+ if (!$this->journal) {
+ throw new Nette\InvalidStateException('CacheJournal has not been provided.');
+ }
+
+ $this->journal->write($key, $dp);
+ }
+
+ $records[$key] = $meta;
+ }
+
+ return $this->memcached->setMulti($records, $expire);
+ }
+
+ public function remove(string $key): void
{
$this->memcached->delete(urlencode($this->prefix . $key), 0);
}
From 595deaabd67cbb3f14253dc384fadc3264f19b81 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 13:39:05 +0100
Subject: [PATCH 02/11] Move properties to constructor and add bulkRemove
method
---
src/Caching/Storages/MemcachedStorage.php | 38 ++++++++++++-----------
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/src/Caching/Storages/MemcachedStorage.php b/src/Caching/Storages/MemcachedStorage.php
index c4bf569..d91eba7 100644
--- a/src/Caching/Storages/MemcachedStorage.php
+++ b/src/Caching/Storages/MemcachedStorage.php
@@ -25,11 +25,9 @@ class MemcachedStorage implements Nette\Caching\Storage, Nette\Caching\BulkReade
MetaDelta = 'delta';
private \Memcached $memcached;
- private string $prefix;
- private ?Journal $journal;
- /**
+ /**
* Checks if Memcached extension is available.
*/
public static function isAvailable(): bool
@@ -38,26 +36,23 @@ public static function isAvailable(): bool
}
- public function __construct(
+ public function __construct(
string $host = 'localhost',
int $port = 11211,
- string $prefix = '',
- ?Journal $journal = null,
- ) {
- if (!static::isAvailable()) {
+ private string $prefix = '',
+ private ?Journal $journal = null
+ ) {
+ if (!static::isAvailable()) {
throw new Nette\NotSupportedException("PHP extension 'memcached' is not loaded.");
- }
-
- $this->prefix = $prefix;
- $this->journal = $journal;
- $this->memcached = new \Memcached;
- if ($host) {
- $this->addServer($host, $port);
- }
- }
+ }
+ $this->memcached = new \Memcached;
+ if ($host) {
+ $this->addServer($host, $port);
+ }
+ }
- public function addServer(string $host = 'localhost', int $port = 11211): void
+ public function addServer(string $host = 'localhost', int $port = 11211): void
{
if (@$this->memcached->addServer($host, $port, 1) === false) { // @ is escalated to exception
$error = error_get_last();
@@ -208,12 +203,19 @@ public function bulkWrite(array $items, array $dp): bool
return $this->memcached->setMulti($records, $expire);
}
+
public function remove(string $key): void
{
$this->memcached->delete(urlencode($this->prefix . $key), 0);
}
+ public function bulkRemove(array $keys): void
+ {
+ $this->memcached->deleteMulti(array_map(fn($key) => urlencode($this->prefix . $key), $keys), 0);
+ }
+
+
public function clean(array $conditions): void
{
if (!empty($conditions[Cache::All])) {
From 2e365e2103ff0b934e024510350f8051bbd1365e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 13:40:22 +0100
Subject: [PATCH 03/11] Add bulkRemove method to interface
---
src/Caching/BulkWriter.php | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/src/Caching/BulkWriter.php b/src/Caching/BulkWriter.php
index d9f9a8a..342c0f1 100644
--- a/src/Caching/BulkWriter.php
+++ b/src/Caching/BulkWriter.php
@@ -26,4 +26,9 @@ interface BulkWriter
* @throws Nette\InvalidStateException
*/
function bulkWrite(array $items, array $dp): bool;
+
+ /**
+ * Removes multiple items from cache
+ */
+ function bulkRemove(array $keys): void;
}
From 8a0682fe1c0959e15f66698cadd3e21eb4af95fd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 13:44:53 +0100
Subject: [PATCH 04/11] Fix whitespaces
---
src/Caching/BulkWriter.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Caching/BulkWriter.php b/src/Caching/BulkWriter.php
index 342c0f1..4a46e29 100644
--- a/src/Caching/BulkWriter.php
+++ b/src/Caching/BulkWriter.php
@@ -16,7 +16,7 @@
interface BulkWriter
{
/**
- * Writes to cache in bulk.
+ * Writes to cache in bulk.
* Similar to write()
, but instead of a single key/value item, it works on multiple items specified in items
*
* @param array{string, mixed} $items An array of key/data pairs to store on the server
From 935c5ac8ff02f73aa9442481a2e603d8e29a71aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 13:47:40 +0100
Subject: [PATCH 05/11] Fix throws statement
---
src/Caching/BulkWriter.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/Caching/BulkWriter.php b/src/Caching/BulkWriter.php
index 4a46e29..6a2a2f4 100644
--- a/src/Caching/BulkWriter.php
+++ b/src/Caching/BulkWriter.php
@@ -22,8 +22,8 @@ interface BulkWriter
* @param array{string, mixed} $items An array of key/data pairs to store on the server
* @param array $dp Global dependencies of each stored value
* @return bool Returns true
on success or false
on failure
- * @throws Nette\NotSupportedException
- * @throws Nette\InvalidStateException
+ * @throws \Nette\NotSupportedException
+ * @throws \Nette\NotSupportedException
*/
function bulkWrite(array $items, array $dp): bool;
From 24731dfe971eee723b4e605dee8a0b8515132517 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 13:53:19 +0100
Subject: [PATCH 06/11] Fix coding style
---
src/Caching/BulkWriter.php | 32 ++++----
src/Caching/Storages/MemcachedStorage.php | 97 ++++++++++++-----------
2 files changed, 66 insertions(+), 63 deletions(-)
diff --git a/src/Caching/BulkWriter.php b/src/Caching/BulkWriter.php
index 6a2a2f4..371e72f 100644
--- a/src/Caching/BulkWriter.php
+++ b/src/Caching/BulkWriter.php
@@ -8,6 +8,8 @@
declare(strict_types=1);
namespace Nette\Caching;
+use Nette\InvalidStateException;
+use Nette\NotSupportedException;
/**
@@ -15,20 +17,20 @@
*/
interface BulkWriter
{
- /**
- * Writes to cache in bulk.
- * Similar to write()
, but instead of a single key/value item, it works on multiple items specified in items
- *
- * @param array{string, mixed} $items An array of key/data pairs to store on the server
- * @param array $dp Global dependencies of each stored value
- * @return bool Returns true
on success or false
on failure
- * @throws \Nette\NotSupportedException
- * @throws \Nette\NotSupportedException
- */
- function bulkWrite(array $items, array $dp): bool;
+ /**
+ * Writes to cache in bulk.
+ * Similar to write()
, but instead of a single key/value item, it works on multiple items specified in items
+ *
+ * @param array{string, mixed} $items An array of key/data pairs to store on the server
+ * @param array $dp Global dependencies of each stored value
+ * @return bool Returns true
on success or false
on failure
+ * @throws NotSupportedException
+ * @throws InvalidStateException
+ */
+ function bulkWrite(array $items, array $dp): bool;
- /**
- * Removes multiple items from cache
- */
- function bulkRemove(array $keys): void;
+ /**
+ * Removes multiple items from cache
+ */
+ function bulkRemove(array $keys): void;
}
diff --git a/src/Caching/Storages/MemcachedStorage.php b/src/Caching/Storages/MemcachedStorage.php
index d91eba7..295783c 100644
--- a/src/Caching/Storages/MemcachedStorage.php
+++ b/src/Caching/Storages/MemcachedStorage.php
@@ -27,7 +27,7 @@ class MemcachedStorage implements Nette\Caching\Storage, Nette\Caching\BulkReade
private \Memcached $memcached;
- /**
+ /**
* Checks if Memcached extension is available.
*/
public static function isAvailable(): bool
@@ -36,23 +36,23 @@ public static function isAvailable(): bool
}
- public function __construct(
+ public function __construct(
string $host = 'localhost',
int $port = 11211,
private string $prefix = '',
- private ?Journal $journal = null
- ) {
- if (!static::isAvailable()) {
+ private ?Journal $journal = null,
+ ) {
+ if (!static::isAvailable()) {
throw new Nette\NotSupportedException("PHP extension 'memcached' is not loaded.");
- }
- $this->memcached = new \Memcached;
- if ($host) {
- $this->addServer($host, $port);
- }
- }
+ }
+ $this->memcached = new \Memcached;
+ if ($host) {
+ $this->addServer($host, $port);
+ }
+ }
- public function addServer(string $host = 'localhost', int $port = 11211): void
+ public function addServer(string $host = 'localhost', int $port = 11211): void
{
if (@$this->memcached->addServer($host, $port, 1) === false) { // @ is escalated to exception
$error = error_get_last();
@@ -162,58 +162,59 @@ public function write(string $key, $data, array $dp): void
$this->memcached->set($key, $meta, $expire);
}
- public function bulkWrite(array $items, array $dp): bool
- {
- if (isset($dp[Cache::Items])) {
- throw new Nette\NotSupportedException('Dependent items are not supported by MemcachedStorage.');
- }
- $records = [];
+ public function bulkWrite(array $items, array $dp): bool
+ {
+ if (isset($dp[Cache::Items])) {
+ throw new Nette\NotSupportedException('Dependent items are not supported by MemcachedStorage.');
+ }
- $expire = 0;
- if (isset($dp[Cache::Expire])) {
- $expire = (int) $dp[Cache::Expire];
- }
+ $records = [];
- foreach ($items as $key => $data) {
- $key = urlencode($this->prefix . $key);
- $meta = [
- self::MetaData => $data,
- ];
+ $expire = 0;
+ if (isset($dp[Cache::Expire])) {
+ $expire = (int) $dp[Cache::Expire];
+ }
- if (!empty($dp[Cache::Sliding])) {
- $meta[self::MetaDelta] = $expire; // sliding time
- }
+ foreach ($items as $key => $data) {
+ $key = urlencode($this->prefix . $key);
+ $meta = [
+ self::MetaData => $data,
+ ];
- if (isset($dp[Cache::Callbacks])) {
- $meta[self::MetaCallbacks] = $dp[Cache::Callbacks];
- }
+ if (!empty($dp[Cache::Sliding])) {
+ $meta[self::MetaDelta] = $expire; // sliding time
+ }
+
+ if (isset($dp[Cache::Callbacks])) {
+ $meta[self::MetaCallbacks] = $dp[Cache::Callbacks];
+ }
- if (isset($dp[Cache::Tags]) || isset($dp[Cache::Priority])) {
- if (!$this->journal) {
- throw new Nette\InvalidStateException('CacheJournal has not been provided.');
- }
+ if (isset($dp[Cache::Tags]) || isset($dp[Cache::Priority])) {
+ if (!$this->journal) {
+ throw new Nette\InvalidStateException('CacheJournal has not been provided.');
+ }
- $this->journal->write($key, $dp);
- }
+ $this->journal->write($key, $dp);
+ }
- $records[$key] = $meta;
- }
+ $records[$key] = $meta;
+ }
- return $this->memcached->setMulti($records, $expire);
- }
+ return $this->memcached->setMulti($records, $expire);
+ }
- public function remove(string $key): void
+ public function remove(string $key): void
{
$this->memcached->delete(urlencode($this->prefix . $key), 0);
}
- public function bulkRemove(array $keys): void
- {
- $this->memcached->deleteMulti(array_map(fn($key) => urlencode($this->prefix . $key), $keys), 0);
- }
+ public function bulkRemove(array $keys): void
+ {
+ $this->memcached->deleteMulti(array_map(fn($key) => urlencode($this->prefix . $key), $keys), 0);
+ }
public function clean(array $conditions): void
From dde99d4b8e3aa5a26233cfcdd36b056ca87040e1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 14:20:08 +0100
Subject: [PATCH 07/11] Add bulkSave method into Cache entity
---
src/Caching/Cache.php | 45 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/src/Caching/Cache.php b/src/Caching/Cache.php
index 245836b..58c3dd4 100644
--- a/src/Caching/Cache.php
+++ b/src/Caching/Cache.php
@@ -173,6 +173,51 @@ public function bulkLoad(array $keys, ?callable $generator = null): array
}
+ /**
+ * Writes multiple items into cache
+ *
+ * @param array $items
+ * @param array|null $dependencies
+ * @return bool
+ *
+ * @throws InvalidArgumentException
+ */
+ public function bulkSave(array $items, ?array $dependencies = null): bool
+ {
+ if (!$this->storage instanceof BulkWriter) {
+ foreach ($items as $key => $data) {
+ $this->save($key, $data, $dependencies ?? []);
+ }
+ return true;
+ }
+
+ $dependencies = $this->completeDependencies($dependencies);
+
+ if (isset($dependencies[self::Expire]) && $dependencies[self::Expire] <= 0) {
+ $this->storage->bulkRemove(array_map(fn($key): string => $this->generateKey($key), array_keys($items)));
+ return true;
+ }
+
+ $removals = [];
+ $storedItems = [];
+ foreach ($items as $key => $data) {
+ $key = $this->generateKey($key);
+
+ if ($data === null) {
+ $removals[] = $key;
+ } else {
+ $storedItems[$key] = $data;
+ }
+ }
+
+ if (!empty($removals)) {
+ $this->storage->bulkRemove($removals);
+ }
+
+ return $this->storage->bulkWrite($storedItems, $dependencies);
+ }
+
+
/**
* Writes item into the cache.
* Dependencies are:
From 94f3f74cc91ea3f0f0e942a5b077d33282ecdc4f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 14:23:49 +0100
Subject: [PATCH 08/11] Return stored items from bulkSave method
---
src/Caching/Cache.php | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/src/Caching/Cache.php b/src/Caching/Cache.php
index 58c3dd4..e12316d 100644
--- a/src/Caching/Cache.php
+++ b/src/Caching/Cache.php
@@ -178,28 +178,30 @@ public function bulkLoad(array $keys, ?callable $generator = null): array
*
* @param array $items
* @param array|null $dependencies
- * @return bool
+ * @return array Stored items
*
* @throws InvalidArgumentException
*/
- public function bulkSave(array $items, ?array $dependencies = null): bool
+ public function bulkSave(array $items, ?array $dependencies = null): array
{
- if (!$this->storage instanceof BulkWriter) {
- foreach ($items as $key => $data) {
- $this->save($key, $data, $dependencies ?? []);
+ $storedItems = [];
+
+ if (!$this->storage instanceof BulkWriter) {
+
+ foreach ($items as $key => $data) {
+ $storedItems[] = $this->save($key, $data, $dependencies ?? []);
}
- return true;
+ return $storedItems;
}
$dependencies = $this->completeDependencies($dependencies);
if (isset($dependencies[self::Expire]) && $dependencies[self::Expire] <= 0) {
$this->storage->bulkRemove(array_map(fn($key): string => $this->generateKey($key), array_keys($items)));
- return true;
+ return [];
}
$removals = [];
- $storedItems = [];
foreach ($items as $key => $data) {
$key = $this->generateKey($key);
@@ -214,7 +216,9 @@ public function bulkSave(array $items, ?array $dependencies = null): bool
$this->storage->bulkRemove($removals);
}
- return $this->storage->bulkWrite($storedItems, $dependencies);
+ $this->storage->bulkWrite($storedItems, $dependencies);
+
+ return $storedItems;
}
From ff90adfcff2f7ad35c75ad6d1da212ddef08df73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 16:46:19 +0100
Subject: [PATCH 09/11] Separate original keys from generatedKeys when
returning cached values
---
src/Caching/Cache.php | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/src/Caching/Cache.php b/src/Caching/Cache.php
index e12316d..bad6f78 100644
--- a/src/Caching/Cache.php
+++ b/src/Caching/Cache.php
@@ -186,10 +186,10 @@ public function bulkSave(array $items, ?array $dependencies = null): array
{
$storedItems = [];
- if (!$this->storage instanceof BulkWriter) {
+ if (!$this->storage instanceof BulkWriter) {
- foreach ($items as $key => $data) {
- $storedItems[] = $this->save($key, $data, $dependencies ?? []);
+ foreach ($items as $key => $data) {
+ $storedItems[$key] = $this->save($key, $data, $dependencies);
}
return $storedItems;
}
@@ -202,13 +202,14 @@ public function bulkSave(array $items, ?array $dependencies = null): array
}
$removals = [];
+ $toCache = [];
foreach ($items as $key => $data) {
- $key = $this->generateKey($key);
+ $cKey = $this->generateKey($key);
if ($data === null) {
- $removals[] = $key;
+ $removals[] = $cKey;
} else {
- $storedItems[$key] = $data;
+ $storedItems[$key] = $toCache[$cKey] = $data;
}
}
@@ -216,9 +217,9 @@ public function bulkSave(array $items, ?array $dependencies = null): array
$this->storage->bulkRemove($removals);
}
- $this->storage->bulkWrite($storedItems, $dependencies);
-
- return $storedItems;
+ $this->storage->bulkWrite($toCache, $dependencies);
+
+ return $storedItems;
}
From 8a162b103e4e560ba58970a1c8d309f603d289b7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 16:47:11 +0100
Subject: [PATCH 10/11] Tests for bulkSave with implementation of
BulkWriteTestStorage
---
tests/Caching/Cache.bulkSave.phpt | 53 +++++++++++++++++++++++++++++++
tests/Caching/Cache.php | 52 ++++++++++++++++++++++++++++++
2 files changed, 105 insertions(+)
create mode 100644 tests/Caching/Cache.bulkSave.phpt
diff --git a/tests/Caching/Cache.bulkSave.phpt b/tests/Caching/Cache.bulkSave.phpt
new file mode 100644
index 0000000..b5eb295
--- /dev/null
+++ b/tests/Caching/Cache.bulkSave.phpt
@@ -0,0 +1,53 @@
+bulkSave([1, 2]), 'data');
+ Assert::same([1 => 'value1', 2 => 'value2'], $cache->bulkSave([1 => 'value1', 2 => 'value2']), 'data');
+
+ $data = $cache->bulkLoad([1, 2]);
+ Assert::same('value1', $data[1]['data']);
+ Assert::same('value2', $data[2]['data']);
+});
+
+test('storage with bulk write support', function () {
+ $storage = new BulkWriteTestStorage;
+ $cache = new Cache($storage, 'ns');
+ Assert::same([1, 2], $cache->bulkSave([1, 2]), 'data');
+ Assert::same([1 => 'value1', 2 => 'value2'], $cache->bulkSave([1 => 'value1', 2 => 'value2']), 'data');
+
+ $data = $cache->bulkLoad([1, 2]);
+ Assert::same('value1', $data[1]['data']);
+ Assert::same('value2', $data[2]['data']);
+});
+
+test('dependencies', function () {
+ $storage = new BulkWriteTestStorage;
+ $cache = new Cache($storage, 'ns');
+ $dependencies = [Cache::Tags => ['tag']];
+ $cache->bulkSave([1 => 'value1', 2 => 'value2'], $dependencies);
+
+ $data = $cache->bulkLoad([1, 2]);
+ Assert::same($dependencies, $data[1]['dependencies']);
+ Assert::same($dependencies, $data[2]['dependencies']);
+
+ $cache->clean($dependencies);
+
+ Assert::same([1 => null, 2 => null], $cache->bulkLoad([1, 2]));
+});
diff --git a/tests/Caching/Cache.php b/tests/Caching/Cache.php
index 57320d8..e918f56 100644
--- a/tests/Caching/Cache.php
+++ b/tests/Caching/Cache.php
@@ -2,6 +2,7 @@
declare(strict_types=1);
+use Nette\Caching\BulkWriter;
use Nette\Caching\IBulkReader;
use Nette\Caching\IStorage;
@@ -37,6 +38,25 @@ public function remove(string $key): void
public function clean(array $conditions): void
{
+ if (!empty($conditions[Nette\Caching\Cache::All])) {
+ $this->data = [];
+ return;
+ }
+
+ //unset based by tags
+ if (!empty($conditions[Nette\Caching\Cache::Tags])) {
+ $unsets = [];
+ foreach ($this->data as $key => $data) {
+ $tags = $data['dependencies'][Nette\Caching\Cache::Tags] ?? null;
+ if (array_intersect($conditions[Nette\Caching\Cache::Tags], $tags)) {
+ $unsets[$key] = $key;
+ }
+ }
+
+ foreach ($unsets as $unsetKey) {
+ unset($this->data[$unsetKey]);
+ }
+ }
}
}
@@ -55,3 +75,35 @@ public function bulkRead(array $keys): array
return $result;
}
}
+
+class BulkWriteTestStorage extends TestStorage implements BulkWriter
+{
+ public function bulkRead(array $keys): array
+ {
+ $result = [];
+ foreach ($keys as $key) {
+ $data = $this->read($key);
+ if ($data !== null) {
+ $result[$key] = $data;
+ }
+ }
+
+ return $result;
+ }
+
+
+ public function bulkRemove(array $keys): void
+ {
+
+ }
+
+
+ public function bulkWrite($items, array $dp): bool
+ {
+ foreach ($items as $key => $data) {
+ $this->write($key, $data, $dp);
+ }
+
+ return true;
+ }
+}
From 5694e14e519d88fea3b4e2445e926117a14771d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?=
Date: Fri, 17 Nov 2023 16:54:13 +0100
Subject: [PATCH 11/11] Test memcached bulkWrite
---
tests/Storages/Memcached.bulkWrite.phpt | 37 +++++++++++++++++++++++++
1 file changed, 37 insertions(+)
create mode 100644 tests/Storages/Memcached.bulkWrite.phpt
diff --git a/tests/Storages/Memcached.bulkWrite.phpt b/tests/Storages/Memcached.bulkWrite.phpt
new file mode 100644
index 0000000..4961667
--- /dev/null
+++ b/tests/Storages/Memcached.bulkWrite.phpt
@@ -0,0 +1,37 @@
+bulkSave(["foo" => "bar"]);
+Assert::same(['foo' => 'bar', 'lorem' => null], $cache->bulkLoad(['foo', 'lorem']));
+
+//tags
+$dependencies = [Cache::Tags => ['tag']];
+$cache->bulkSave(["foo" => "bar"], $dependencies);
+Assert::same(['foo' => 'bar'], $cache->bulkLoad(['foo']));
+$cache->clean($dependencies);
+Assert::same(['foo' => null], $cache->bulkLoad(['foo']));
\ No newline at end of file