Merge pull request #8365 from cakephp/issue-8364

2.x - Handle negative numbers in Redis correctly.
This commit is contained in:
José Lorenzo Rodríguez 2016-02-27 16:42:08 +01:00
commit da15674a2c
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
*