Skip to content

Commit c92b6e0

Browse files
committed
fix(Cache): handle unavailable EVAL and preserve Predis exception behavior
RedisHandler: treat `false` from phpredis EVAL (e.g. EVAL disabled via `rename-command`) as unavailable and fall back to sequential hMset and expireAt. Integer 0 still means HMSET failed inside the script and must not trigger the fallback. PredisHandler: call HMSET and EXPIREAT with redis.call so a WRONGTYPE error aborts the script before EXPIREAT and is propagated to the client as ServerException again. Add regression tests for both behaviors.
1 parent 061a1f3 commit c92b6e0

4 files changed

Lines changed: 73 additions & 13 deletions

File tree

‎system/Cache/Handlers/PredisHandler.php‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@
2828
class PredisHandler extends BaseHandler
2929
{
3030
/**
31-
* Lua script to conditionally execute HMSET and EXPIREAT in a single round trip.
32-
* Prevents EXPIREAT from mutating TTL or expiring keys when HMSET fails (e.g. WRONGTYPE).
31+
* Lua script to execute HMSET and EXPIREAT in a single round trip.
32+
* An HMSET error (e.g. WRONGTYPE) aborts the script before EXPIREAT runs and
33+
* is propagated to the client as an exception reply.
3334
*/
3435
private const SAVE_SCRIPT = <<<'LUA'
35-
local res = redis.pcall('HMSET', KEYS[1], '__ci_type', ARGV[2], '__ci_value', ARGV[3])
36-
if type(res) == 'table' and res.err then
37-
return 0
38-
end
36+
redis.call('HMSET', KEYS[1], '__ci_type', ARGV[2], '__ci_value', ARGV[3])
3937
redis.call('EXPIREAT', KEYS[1], ARGV[1])
4038
return 1
4139
LUA;

‎system/Cache/Handlers/RedisHandler.php‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,27 @@ public function save(string $key, mixed $value, int $ttl = 60): bool
154154
$expiresAt = Time::now()->getTimestamp() + $ttl;
155155

156156
try {
157-
return (bool) $this->redis->eval(
157+
$result = $this->redis->eval(
158158
self::SAVE_SCRIPT,
159159
[$key, $expiresAt, $dataType, (string) $value],
160160
1,
161161
);
162+
163+
// phpredis returns false when EVAL is unavailable (e.g. disabled via
164+
// rename-command). Integer 0 means HMSET failed inside the script and
165+
// must not trigger the fallback.
166+
if ($result !== false) {
167+
return (bool) $result;
168+
}
162169
} catch (Throwable) {
163170
// If EVAL is unsupported or blocked, fallback to sequential execution.
164-
if (! $this->redis->hMset($key, $fields)) {
165-
return false;
166-
}
171+
}
167172

168-
return $this->redis->expireAt($key, $expiresAt);
173+
if (! $this->redis->hMset($key, $fields)) {
174+
return false;
169175
}
176+
177+
return $this->redis->expireAt($key, $expiresAt);
170178
}
171179

172180
return (bool) $this->redis->hMset($key, $fields);

‎tests/system/Cache/Handlers/PredisHandlerTest.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Config\Cache;
2020
use PHPUnit\Framework\Attributes\Group;
2121
use Predis\Client;
22+
use Predis\Response\ServerException;
2223
use RuntimeException;
2324

2425
/**
@@ -199,7 +200,7 @@ public function testReconnect(): void
199200
$this->assertSame('value', $this->handler->get(self::$key1));
200201
}
201202

202-
public function testSaveFailsOnNonHashKeyPreservingValueAndTtl(): void
203+
public function testSaveThrowsServerExceptionOnNonHashKeyPreservingValueAndTtl(): void
203204
{
204205
/** @var Client $redis */
205206
$redis = $this->getPrivateProperty($this->handler, 'redis');
@@ -208,7 +209,12 @@ public function testSaveFailsOnNonHashKeyPreservingValueAndTtl(): void
208209
$redis->set($prefixedKey, 'raw_string_value');
209210
$redis->expire($prefixedKey, 300);
210211

211-
$this->assertFalse($this->handler->save('non_hash_key', 'new_value', 60));
212+
try {
213+
$this->handler->save('non_hash_key', 'new_value', 60);
214+
$this->fail('Predis\Response\ServerException was not thrown for a non-hash key.');
215+
} catch (ServerException $e) {
216+
$this->assertSame('WRONGTYPE', $e->getErrorType());
217+
}
212218

213219
$this->assertSame('raw_string_value', $redis->get($prefixedKey));
214220
$this->assertGreaterThan(250, $redis->ttl($prefixedKey));

‎tests/system/Cache/Handlers/RedisHandlerTest.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,52 @@ public function testSaveFallbackWhenEvalThrowsException(): void
286286
$this->setPrivateProperty($this->handler, 'redis', $originalRedis);
287287
}
288288
}
289+
290+
public function testSaveFallbackWhenEvalReturnsFalse(): void
291+
{
292+
/** @var Redis $originalRedis */
293+
$originalRedis = $this->getPrivateProperty($this->handler, 'redis');
294+
295+
$failingRedis = $this->createMock(Redis::class);
296+
$failingRedis->expects($this->once())
297+
->method('eval')
298+
->willReturn(false);
299+
$failingRedis->expects($this->once())
300+
->method('hMset')
301+
->willReturn(true);
302+
$failingRedis->expects($this->once())
303+
->method('expireAt')
304+
->willReturn(true);
305+
306+
try {
307+
$this->setPrivateProperty($this->handler, 'redis', $failingRedis);
308+
309+
$this->assertTrue($this->handler->save(self::$key1, 'fallback_value', 120));
310+
} finally {
311+
$this->setPrivateProperty($this->handler, 'redis', $originalRedis);
312+
}
313+
}
314+
315+
public function testSaveDoesNotFallbackWhenScriptReportsHmsetFailure(): void
316+
{
317+
/** @var Redis $originalRedis */
318+
$originalRedis = $this->getPrivateProperty($this->handler, 'redis');
319+
320+
$failingRedis = $this->createMock(Redis::class);
321+
$failingRedis->expects($this->once())
322+
->method('eval')
323+
->willReturn(0);
324+
$failingRedis->expects($this->never())
325+
->method('hMset');
326+
$failingRedis->expects($this->never())
327+
->method('expireAt');
328+
329+
try {
330+
$this->setPrivateProperty($this->handler, 'redis', $failingRedis);
331+
332+
$this->assertFalse($this->handler->save(self::$key1, 'new_value', 60));
333+
} finally {
334+
$this->setPrivateProperty($this->handler, 'redis', $originalRedis);
335+
}
336+
}
289337
}

0 commit comments

Comments
 (0)