Skip to content

Commit

Permalink
Handle negative numbers in Redis correctly.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed Feb 27, 2016
1 parent 1804d87 commit 056e99e
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 056e99e

Please sign in to comment.