|
7 | 7 | * For the full copyright and license information, please view the LICENSE |
8 | 8 | * file that was distributed with this source code. |
9 | 9 | */ |
| 10 | + |
10 | 11 | namespace xPDO\Cache; |
| 12 | + |
11 | 13 | use xPDO\xPDO; |
12 | 14 | use Redis; |
13 | 15 |
|
|
18 | 20 | * |
19 | 21 | * @package xPDO\Cache |
20 | 22 | */ |
21 | | -class xPDORedisCache extends xPDOCache { |
| 23 | +class xPDORedisCache extends xPDOCache |
| 24 | +{ |
22 | 25 | protected $redis = null; |
23 | 26 |
|
24 | | - public function __construct(& $xpdo, $options = array()) { |
25 | | - parent :: __construct($xpdo, $options); |
| 27 | + public function __construct(&$xpdo, $options = []) |
| 28 | + { |
| 29 | + parent:: __construct($xpdo, $options); |
26 | 30 | if (class_exists('Redis', true)) { |
27 | | - $this->redis= new Redis(); |
| 31 | + $this->redis = new Redis(); |
28 | 32 | 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); |
34 | 45 | } |
35 | 46 | $this->redis->select((integer)$this->getOption('redis_db', $options, 0)); |
36 | 47 | $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); |
37 | | - $this->initialized = true; |
38 | | - } |
| 48 | + $this->initialized = true; |
| 49 | + } |
39 | 50 | } else { |
40 | 51 | $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 | + ); |
42 | 60 | } |
43 | 61 | } 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 | + ); |
45 | 66 | } |
46 | 67 | } |
47 | 68 |
|
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); |
52 | 74 | } |
53 | 75 | return $added; |
54 | 76 | } |
55 | 77 |
|
56 | | - public function set($key, $var, $expire= 0, $options= array()) { |
| 78 | + public function set($key, $var, $expire = 0, $options = []) |
| 79 | + { |
57 | 80 | if ($expire === 0) { |
58 | | - $set = $this->redis->set($this->getCacheKey($key),$var); |
| 81 | + $set = $this->redis->set($this->getCacheKey($key), $var); |
59 | 82 | } else { |
60 | | - $set = $this->redis->set($this->getCacheKey($key),$var,$expire); |
| 83 | + $set = $this->redis->set($this->getCacheKey($key), $var, $expire); |
61 | 84 | } |
62 | 85 | return $set; |
63 | 86 | } |
64 | 87 |
|
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); |
69 | 93 | } |
70 | 94 | return $replaced; |
71 | 95 | } |
72 | 96 |
|
73 | | - public function delete($key, $options= array()) { |
| 97 | + public function delete($key, $options = []) |
| 98 | + { |
74 | 99 | if ($this->getOption(xPDO::OPT_CACHE_MULTIPLE_OBJECT_DELETE, $options, false)) { |
75 | | - $deleted= $this->flush($options); |
| 100 | + $deleted = $this->flush($options); |
76 | 101 | } else { |
77 | | - $deleted= $this->redis->delete($this->getCacheKey($key)); |
| 102 | + $deleted = $this->redis->delete($this->getCacheKey($key)); |
78 | 103 | } |
79 | 104 |
|
80 | 105 | return $deleted; |
81 | 106 | } |
82 | 107 |
|
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)); |
85 | 111 | return $value; |
86 | 112 | } |
87 | 113 |
|
88 | | - public function flush($options= array()) { |
| 114 | + public function flush($options = []) |
| 115 | + { |
89 | 116 | return $this->redis->flushDb(); |
90 | 117 | } |
91 | 118 | } |
0 commit comments