mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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:
parent
eae7a8926b
commit
3ed321dff4
2 changed files with 19 additions and 3 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue