Skip to content

Commit 0512281

Browse files
Bournwogopengeek
andauthored
xPDORedisCache expire can't be zero (#261)
* Update xPDORedisCache.php * Update formatting --------- Co-authored-by: Jason Coward <[email protected]>
1 parent 2c126f9 commit 0512281

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed

src/xPDO/Cache/xPDORedisCache.php

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace xPDO\Cache;
12+
1113
use xPDO\xPDO;
1214
use Redis;
1315

@@ -18,74 +20,99 @@
1820
*
1921
* @package xPDO\Cache
2022
*/
21-
class xPDORedisCache extends xPDOCache {
23+
class xPDORedisCache extends xPDOCache
24+
{
2225
protected $redis = null;
2326

24-
public function __construct(& $xpdo, $options = array()) {
25-
parent :: __construct($xpdo, $options);
27+
public function __construct(&$xpdo, $options = [])
28+
{
29+
parent:: __construct($xpdo, $options);
2630
if (class_exists('Redis', true)) {
27-
$this->redis= new Redis();
31+
$this->redis = new Redis();
2832
if ($this->redis) {
29-
$server = explode(':', $this->getOption($this->key . '_redis_server', $options, $this->getOption('redis_server', $options, 'localhost:6379')));
30-
if($this->redis->pconnect($server[0], (integer) $server[1])){
31-
$redis_auth=$this->getOption('redis_auth', $options, '');
32-
if(!empty($redis_auth)){
33-
$this->redis->auth($redis_auth);
33+
$server = explode(
34+
':',
35+
$this->getOption(
36+
$this->key . '_redis_server',
37+
$options,
38+
$this->getOption('redis_server', $options, 'localhost:6379')
39+
)
40+
);
41+
if ($this->redis->pconnect($server[0], (integer)$server[1])) {
42+
$redis_auth = $this->getOption('redis_auth', $options, '');
43+
if (!empty($redis_auth)) {
44+
$this->redis->auth($redis_auth);
3445
}
3546
$this->redis->select((integer)$this->getOption('redis_db', $options, 0));
3647
$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
37-
$this->initialized = true;
38-
}
48+
$this->initialized = true;
49+
}
3950
} else {
4051
$this->redis = null;
41-
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDORedisCache[{$this->key}]: Error creating redis provider for server(s): " . $this->getOption($this->key . '_redisd_server', $options, $this->getOption('redisd_server', $options, 'localhost:6379')));
52+
$this->xpdo->log(
53+
xPDO::LOG_LEVEL_ERROR,
54+
"xPDORedisCache[{$this->key}]: Error creating redis provider for server(s): " . $this->getOption(
55+
$this->key . '_redisd_server',
56+
$options,
57+
$this->getOption('redisd_server', $options, 'localhost:6379')
58+
)
59+
);
4260
}
4361
} else {
44-
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDORedisCache[{$this->key}]: Error creating redis provider; xPDORedisCache requires the PHP redis extension.");
62+
$this->xpdo->log(
63+
xPDO::LOG_LEVEL_ERROR,
64+
"xPDORedisCache[{$this->key}]: Error creating redis provider; xPDORedisCache requires the PHP redis extension."
65+
);
4566
}
4667
}
4768

48-
public function add($key, $var, $expire= 0, $options= array()) {
49-
$added= false;
50-
if(!$this->redis->exists($this->getCacheKey($key))){
51-
$added=$this->redis->set($this->getCacheKey($key),$var,$expire);
69+
public function add($key, $var, $expire = 0, $options = [])
70+
{
71+
$added = false;
72+
if (!$this->redis->exists($this->getCacheKey($key))) {
73+
$added = $this->set($key, $var, $expire);
5274
}
5375
return $added;
5476
}
5577

56-
public function set($key, $var, $expire= 0, $options= array()) {
78+
public function set($key, $var, $expire = 0, $options = [])
79+
{
5780
if ($expire === 0) {
58-
$set = $this->redis->set($this->getCacheKey($key),$var);
81+
$set = $this->redis->set($this->getCacheKey($key), $var);
5982
} else {
60-
$set = $this->redis->set($this->getCacheKey($key),$var,$expire);
83+
$set = $this->redis->set($this->getCacheKey($key), $var, $expire);
6184
}
6285
return $set;
6386
}
6487

65-
public function replace($key, $var, $expire= 0, $options= array()) {
66-
$replaced=false;
67-
if($this->redis->exists($this->getCacheKey($key))){
68-
$replaced=$this->redis->set($this->getCacheKey($key),$var,$expire);
88+
public function replace($key, $var, $expire = 0, $options = [])
89+
{
90+
$replaced = false;
91+
if ($this->redis->exists($this->getCacheKey($key))) {
92+
$replaced = $this->set($key, $var, $expire);
6993
}
7094
return $replaced;
7195
}
7296

73-
public function delete($key, $options= array()) {
97+
public function delete($key, $options = [])
98+
{
7499
if ($this->getOption(xPDO::OPT_CACHE_MULTIPLE_OBJECT_DELETE, $options, false)) {
75-
$deleted= $this->flush($options);
100+
$deleted = $this->flush($options);
76101
} else {
77-
$deleted= $this->redis->delete($this->getCacheKey($key));
102+
$deleted = $this->redis->delete($this->getCacheKey($key));
78103
}
79104

80105
return $deleted;
81106
}
82107

83-
public function get($key, $options= array()) {
84-
$value= $this->redis->get($this->getCacheKey($key));
108+
public function get($key, $options = [])
109+
{
110+
$value = $this->redis->get($this->getCacheKey($key));
85111
return $value;
86112
}
87113

88-
public function flush($options= array()) {
114+
public function flush($options = [])
115+
{
89116
return $this->redis->flushDb();
90117
}
91118
}

0 commit comments

Comments
 (0)