Skip to content
Permalink
Browse files

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
  • Loading branch information...
markstory committed Feb 27, 2016
1 parent 1804d87 commit 056e99e1718a3fbd1401f3135e2439b66706267a
Showing with 20 additions and 3 deletions.
  1. +3 −3 src/Cache/Engine/RedisEngine.php
  2. +17 −0 tests/TestCase/Cache/Engine/RedisEngineTest.php
@@ -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;
}
@@ -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
*

0 comments on commit 056e99e

Please sign in to comment.
You can’t perform that action at this time.