Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit 3ed321d

Browse files
committed
Handle negative numbers in Redis correctly.
Update number sniff to handle negative numbers. We need to do number sniffing so we can maintain compatbility between write() and increment()/decrement(). Refs #8364
1 parent eae7a89 commit 3ed321d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Cake/Cache/Engine/RedisEngine.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ public function write($key, $value, $duration) {
133133
*/
134134
public function read($key) {
135135
$value = $this->_Redis->get($key);
136-
if (ctype_digit($value)) {
137-
$value = (int)$value;
136+
if (preg_match('/^[-]?\d+$/', $value)) {
137+
return (int)$value;
138138
}
139139
if ($value !== false && is_string($value)) {
140-
$value = unserialize($value);
140+
return unserialize($value);
141141
}
142142
return $value;
143143
}

lib/Cake/Test/Case/Cache/Engine/RedisEngineTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ public function testMultiDatabaseOperations() {
144144
Cache::drop('redisdb1');
145145
}
146146

147+
/**
148+
* test write numbers method
149+
*
150+
* @return void
151+
*/
152+
public function testWriteNumbers() {
153+
$result = Cache::write('test-counter', 1, 'redis');
154+
$this->assertSame(1, Cache::read('test-counter', 'redis'));
155+
156+
$result = Cache::write('test-counter', 0, 'redis');
157+
$this->assertSame(0, Cache::read('test-counter', 'redis'));
158+
159+
$result = Cache::write('test-counter', -1, 'redis');
160+
$this->assertSame(-1, Cache::read('test-counter', 'redis'));
161+
}
162+
147163
/**
148164
* testReadAndWriteCache method
149165
*

0 commit comments

Comments
 (0)