Skip to content

Commit

Permalink
[NFR] Add cache timeout option support to the redis adapter (phalcon#…
Browse files Browse the repository at this point in the history
…13414)

* feature: add redis cache timeout options

* fix: add timeout  parameter

*  test:add redis timeout test

* docs: add changelog

* Update change log [ci skip]
  • Loading branch information
cq-z authored and sergeyklay committed Jun 25, 2018
1 parent dd2e91c commit 480a9c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-3.4.md
@@ -1,4 +1,5 @@
# [3.4.1](https://github.com/phalcon/cphalcon/releases/tag/v3.4.1) (2018-XX-XX)
- Changed `Phalcon\Cache\Backend\Redis` to support connection timeout parameter
- Fixed `Phalcon\Validaiton\Validator\Uniqueness::isUniquenessModel` to properly get value of primary key when it has different name in column map [#13398](https://github.com/phalcon/cphalcon/issues/13398)
- Fixed bad performance for repeated `Phalcon\Mvc\Router::getRouteByName` and `Phalcon\Mvc\Router::getRouteById` calls for applications with many routes

Expand Down
12 changes: 7 additions & 5 deletions phalcon/cache/backend/redis.zep
Expand Up @@ -100,7 +100,9 @@ class Redis extends Backend
if !isset options["auth"] {
let options["auth"] = "";
}

if !isset options["timeout"] {
let options["timeout"] = 0;
}
parent::__construct(frontend, options);
}

Expand All @@ -109,19 +111,19 @@ class Redis extends Backend
*/
public function _connect()
{
var options, redis, persistent, success, host, port, auth, index;
var options, redis, persistent, success, host, port, auth, index, timeout;

let options = this->_options;
let redis = new \Redis();

if !fetch host, options["host"] || !fetch port, options["port"] || !fetch persistent, options["persistent"] {
if !fetch host, options["host"] || !fetch port, options["port"] || !fetch persistent, options["persistent"] || !fetch timeout, options["timeout"] {
throw new Exception("Unexpected inconsistency in options");
}

if persistent {
let success = redis->pconnect(host, port);
let success = redis->pconnect(host, port, timeout);
} else {
let success = redis->connect(host, port);
let success = redis->connect(host, port, timeout);
}

if !success {
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/Cache/Backend/RedisCest.php
Expand Up @@ -365,4 +365,31 @@ public function output(UnitTester $I)
$I->assertNull($content);
$I->dontSeeInRedis('_PHCR' . 'test-output');
}

public function setTimeout(UnitTester $I)
{
$I->wantTo('Get data by using Redis as cache backend and set timeout');

$key = '_PHCR' . 'data-get-timeout';
$data = [uniqid(), gethostname(), microtime(), get_include_path(), time()];

$cache = new Redis(new Data(['lifetime' => 20]), [
'host' => env('TEST_RS_HOST', '127.0.0.1'),
'port' => env('TEST_RS_PORT', 6379),
'index' => env('TEST_RS_DB', 0),
'timeout' => 1,
]);

$I->haveInRedis('string', $key, serialize($data));
$I->assertEquals($data, $cache->get('data-get-timeout'));

$I->assertNull($cache->get($key));

$data = 'sure, nothing interesting';

$I->haveInRedis('string', $key, serialize($data));
$I->assertEquals($data, $cache->get('data-get-timeout'));

$I->assertNull($cache->get($key));
}
}

0 comments on commit 480a9c2

Please sign in to comment.