MDL-82511 cachestore_redis: Reduce blocking when finding keys

This commit is contained in:
Benjamin Walker
2025-09-25 14:23:57 +10:00
parent e7cf1c6130
commit bcf0384d05
+23 -8
View File
@@ -714,13 +714,34 @@ class cachestore_redis extends store implements
return false;
}
/**
* Finds all of the keys being used by this cache store instance using a scan.
* This is preferred over keys to avoid blocking the server for a long time.
*
* @param string $prefix
* @return array of all matching keys in the hash as a numbered array.
*/
protected function scan_keys($prefix = '') {
$return = [];
$iterator = null;
do {
$results = $this->redis->hScan($this->hash, $iterator, "$prefix*", 1000);
if ($results !== false) {
foreach ($results as $key => $value) {
$return[] = $key;
}
}
} while ($iterator != 0);
return $return;
}
/**
* Finds all of the keys being used by this cache store instance.
*
* @return array of all keys in the hash as a numbered array.
*/
public function find_all() {
return $this->redis->hKeys($this->hash);
return $this->scan_keys();
}
/**
@@ -731,13 +752,7 @@ class cachestore_redis extends store implements
* @return array List of keys that match this prefix.
*/
public function find_by_prefix($prefix) {
$return = [];
foreach ($this->find_all() as $key) {
if (strpos($key, $prefix) === 0) {
$return[] = $key;
}
}
return $return;
return $this->scan_keys($prefix);
}
/**