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
This commit is contained in:
mark_story 2016-02-27 00:07:58 -05:00
parent eae7a8926b
commit 3ed321dff4
2 changed files with 19 additions and 3 deletions

View file

@ -133,11 +133,11 @@ class RedisEngine extends CacheEngine {
*/
public function read($key) {
$value = $this->_Redis->get($key);
if (ctype_digit($value)) {
$value = (int)$value;
if (preg_match('/^[-]?\d+$/', $value)) {
return (int)$value;
}
if ($value !== false && is_string($value)) {
$value = unserialize($value);
return unserialize($value);
}
return $value;
}

View file

@ -144,6 +144,22 @@ class RedisEngineTest extends CakeTestCase {
Cache::drop('redisdb1');
}
/**
* test write numbers method
*
* @return void
*/
public function testWriteNumbers() {
$result = Cache::write('test-counter', 1, 'redis');
$this->assertSame(1, Cache::read('test-counter', 'redis'));
$result = Cache::write('test-counter', 0, 'redis');
$this->assertSame(0, Cache::read('test-counter', 'redis'));
$result = Cache::write('test-counter', -1, 'redis');
$this->assertSame(-1, Cache::read('test-counter', 'redis'));
}
/**
* testReadAndWriteCache method
*