Skip to content

Commit

Permalink
Add test for redis increment/decrement setting TTL
Browse files Browse the repository at this point in the history
Also update doc blocks to acknowledge that TTLs are set now.

Refs #10786
  • Loading branch information
markstory committed Jun 20, 2017
1 parent d389b14 commit 9b891c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/Cache/Engine/RedisEngine.php
Expand Up @@ -165,7 +165,7 @@ public function read($key)
}

/**
* Increments the value of an integer cached key
* Increments the value of an integer cached key & update the expiry time
*
* @param string $key Identifier for the data
* @param int $offset How much to increment
Expand All @@ -177,14 +177,13 @@ public function increment($key, $offset = 1)
$key = $this->_key($key);

$value = (int)$this->_Redis->incrBy($key, $offset);

$this->_Redis->setTimeout($key, $duration);

return $value;
}

/**
* Decrements the value of an integer cached key
* Decrements the value of an integer cached key & update the expiry time
*
* @param string $key Identifier for the data
* @param int $offset How much to subtract
Expand All @@ -196,7 +195,6 @@ public function decrement($key, $offset = 1)
$key = $this->_key($key);

$value = (int)$this->_Redis->decrBy($key, $offset);

$this->_Redis->setTimeout($key, $duration);

return $value;
Expand Down
24 changes: 21 additions & 3 deletions tests/TestCase/Cache/Engine/RedisEngineTest.php
@@ -1,8 +1,6 @@
<?php
/**
* RedisEngineTest file
*
* CakePHP(tm) Tests <https://book.cakephp.org/view/1196/Testing>
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
Expand Down Expand Up @@ -341,6 +339,26 @@ public function testIncrement()
$this->assertEquals(3, $result);
}

/**
* Test that increment and decrement set ttls.
*
* @return void
*/
public function testIncrementDecrementExpiring()
{
$this->_configCache(['duration' => 1]);
Cache::delete('test_increment', 'redis');
Cache::delete('test_decrement', 'redis');

$this->assertSame(1, Cache::increment('test_increment', 1, 'redis'));
$this->assertSame(-1, Cache::decrement('test_decrement', 1, 'redis'));

sleep(1);

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

/**
* test clearing redis.
*
Expand Down

0 comments on commit 9b891c8

Please sign in to comment.