Skip to content

Commit 588e8ff

Browse files
authored
Merge pull request #913 from mapcentia/redis_scan_fix
Refactor Redis driver to read all keys efficiently
2 parents 8cadd6f + bcbf5b8 commit 588e8ff

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

.github/workflows/testsv2.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
timeout-minutes: 60
88
strategy:
99
matrix:
10-
operating-system: [ubuntu-latest]
10+
operating-system: [ubuntu-22.04]
1111
php-versions: ['8.0', '8.1', '8.2', '8.3']
1212
name: PHP ${{ matrix.php-versions }} quality/tests on ${{ matrix.operating-system }}
1313
env:

lib/Phpfastcache/Drivers/Redis/Driver.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,15 @@ protected function driverConnect(): bool
125125
*/
126126
protected function driverReadAllKeys(string $pattern = '*'): iterable
127127
{
128-
$i = -1;
129-
$keys = $this->instance->scan($i, $pattern === '' ? '*' : $pattern, ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT);
130-
if (is_iterable($keys)) {
131-
return $keys;
132-
} else {
133-
return [];
134-
}
128+
$i = null;
129+
$pattern = $pattern === '' ? '*' : $pattern;
130+
do {
131+
$keys[] = $this->instance->scan($i, $pattern);
132+
if (\count($keys) > ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT) {
133+
break;
134+
}
135+
} while ($i > 0);
136+
return \array_merge([], ...$keys);
135137
}
136138

137139
/**

lib/Phpfastcache/Drivers/Rediscluster/Driver.php

+12-14
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function getStats(): DriverStatistic
6262
trim(<<<EOF
6363
Redis Cluster version v%s, php-ext v%s with %d master nodes and %d slaves connected are up since %s.
6464
For more information see RawData.
65-
EOF),
65+
EOF
66+
),
6667
implode(', ', array_unique(array_column($infos, 'redis_version'))),
6768
\phpversion("redis"),
6869
count($masters),
@@ -123,21 +124,18 @@ protected function driverConnect(): bool
123124
*/
124125
protected function driverReadAllKeys(string $pattern = '*'): iterable
125126
{
126-
$keys = [[]];
127+
$result = [[]];
127128
foreach ($this->instance->_masters() as $master) {
128-
$i = -1;
129-
$result = $this->instance->scan(
130-
$i,
131-
$master,
132-
$pattern === '' ? '*' : $pattern,
133-
ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT
134-
);
135-
if (is_array($result)) {
136-
$keys[] = $result;
137-
}
129+
$i = 0;
130+
$pattern = $pattern === '' ? '*' : $pattern;
131+
do {
132+
$result[] = $this->instance->scan($i, $master, $pattern);
133+
if (\count($result) > ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT) {
134+
break;
135+
}
136+
} while ($i > 0);
138137
}
139-
140-
return array_unique(array_merge(...$keys));
138+
return array_unique(\array_merge(...$result));
141139
}
142140

143141
/**

0 commit comments

Comments
 (0)