Skip to content

Commit

Permalink
Merge pull request #11136 from cakephp/redis-increment-3x
Browse files Browse the repository at this point in the history
Fix redis increment/decrement with infinite timeout.
  • Loading branch information
markstory committed Sep 2, 2017
2 parents 8e2f1f9 + 2852a07 commit f21bbd1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Cache/Engine/RedisEngine.php
Expand Up @@ -177,7 +177,9 @@ public function increment($key, $offset = 1)
$key = $this->_key($key);

$value = (int)$this->_Redis->incrBy($key, $offset);
$this->_Redis->setTimeout($key, $duration);
if ($duration > 0) {
$this->_Redis->setTimeout($key, $duration);
}

return $value;
}
Expand All @@ -195,7 +197,9 @@ public function decrement($key, $offset = 1)
$key = $this->_key($key);

$value = (int)$this->_Redis->decrBy($key, $offset);
$this->_Redis->setTimeout($key, $duration);
if ($duration > 0) {
$this->_Redis->setTimeout($key, $duration);
}

return $value;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Cache/Engine/RedisEngineTest.php
Expand Up @@ -339,6 +339,27 @@ public function testIncrement()
$this->assertEquals(3, $result);
}

/**
* Test that increment() and decrement() can live forever.
*
* @return void
*/
public function testIncrementDecrementForvever()
{
$this->_configCache(['duration' => 0]);
Cache::delete('test_increment', 'redis');
Cache::delete('test_decrement', 'redis');

$result = Cache::increment('test_increment', 1, 'redis');
$this->assertEquals(1, $result);

$result = Cache::decrement('test_decrement', 1, 'redis');
$this->assertEquals(-1, $result);

$this->assertEquals(1, Cache::read('test_increment', 'redis'));
$this->assertEquals(-1, Cache::read('test_decrement', 'redis'));
}

/**
* Test that increment and decrement set ttls.
*
Expand Down

0 comments on commit f21bbd1

Please sign in to comment.