From edf3f42ae1d70f887f9281339ed737662a3b48e4 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Sat, 20 Dec 2025 15:00:48 -0500 Subject: [PATCH] Optimize `clear()` method in `RedisCacheEngine` using SCAN for improved performance; add Docker service run configuration for tests. --- .run/Services for Test.run.xml | 10 ++++++++++ src/Psr16/RedisCacheEngine.php | 11 ++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 .run/Services for Test.run.xml diff --git a/.run/Services for Test.run.xml b/.run/Services for Test.run.xml new file mode 100644 index 0000000..d8e1671 --- /dev/null +++ b/.run/Services for Test.run.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/Psr16/RedisCacheEngine.php b/src/Psr16/RedisCacheEngine.php index e5d78b6..20b9d26 100644 --- a/src/Psr16/RedisCacheEngine.php +++ b/src/Psr16/RedisCacheEngine.php @@ -159,12 +159,13 @@ public function delete(string $key): bool #[\Override] public function clear(): bool { - $keys = $this->redis->keys('cache:*'); - foreach ($keys as $key) { - if (preg_match('/^cache:(?.*)/', $key, $matches)) { - $this->delete($matches['key']); + $iterator = null; + do { + $keys = $this->redis->scan($iterator, 'cache:*'); + foreach($keys as $key) { + $this->delete(substr($key, 6)); } - } + } while($iterator !== 0); return true; }