Browse Source

[REDIS] Use SCAN instead of KEYS to find all matching keys (#13405)

Use SCAN instead of KEYS to find all matching keys
Anne van de Venis 6 years ago
parent
commit
90440a67c5
2 changed files with 19 additions and 5 deletions
  1. 1 0
      appveyor.yml
  2. 18 5
      src/Cache/Engine/RedisEngine.php

+ 1 - 0
appveyor.yml

@@ -57,6 +57,7 @@ install:
 
   - cd C:\projects\cakephp
   - appveyor-retry appveyor DownloadFile https://getcomposer.org/composer.phar
+  - del composer.lock
   - php composer.phar install --no-progress
   - php -i | grep "ICU version"
 

+ 18 - 5
src/Cache/Engine/RedisEngine.php

@@ -228,14 +228,27 @@ class RedisEngine extends CacheEngine
         if ($check) {
             return true;
         }
-        $keys = $this->_Redis->getKeys($this->_config['prefix'] . '*');
 
-        $result = [];
-        foreach ($keys as $key) {
-            $result[] = $this->_Redis->del($key) > 0;
+        $this->_Redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
+
+        $isAllDeleted = true;
+        $iterator = null;
+        $pattern = $this->_config['prefix'] . '*';
+
+        while (true) {
+            $keys = $this->_Redis->scan($iterator, $pattern);
+
+            if ($keys === false) {
+                break;
+            }
+
+            foreach ($keys as $key) {
+                $isDeleted = ($this->_Redis->del($key) > 0);
+                $isAllDeleted = $isAllDeleted && $isDeleted;
+            }
         }
 
-        return !in_array(false, $result);
+        return $isAllDeleted;
     }
 
     /**