Skip to content

Commit

Permalink
Merge pull request #8366 from cakephp/issue-8364-3x
Browse files Browse the repository at this point in the history
3.x - Handle negative numbers in Redis correctly.
  • Loading branch information
lorenzo committed Feb 27, 2016
2 parents d378f11 + 056e99e commit b615cb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Cache/Engine/RedisEngine.php
Expand Up @@ -153,11 +153,11 @@ public function read($key)
$key = $this->_key($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;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Cache/Engine/RedisEngineTest.php
Expand Up @@ -192,6 +192,23 @@ public function testMultiDatabaseOperations()
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
*
Expand Down

0 comments on commit b615cb1

Please sign in to comment.