1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Cache Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Cache; |
10
|
|
|
|
11
|
|
|
use Joomla\Cache\Item\HasExpirationDateInterface; |
12
|
|
|
use Joomla\Cache\Item\Item; |
13
|
|
|
use Psr\Cache\CacheItemInterface; |
14
|
|
|
use Redis as RedisDriver; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Redis cache driver for the Joomla Framework. |
18
|
|
|
* |
19
|
|
|
* @since 1.0 |
20
|
|
|
*/ |
21
|
|
|
class Redis extends Cache |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Default hostname of redis server |
25
|
|
|
*/ |
26
|
|
|
const REDIS_HOST = '127.0.0.1'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Default port of redis server |
30
|
|
|
*/ |
31
|
|
|
const REDIS_PORT = 6379; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Redis The redis driver. |
35
|
|
|
* @since 1.0 |
36
|
|
|
*/ |
37
|
|
|
private $driver; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* This will wipe out the entire cache's keys |
41
|
|
|
* |
42
|
|
|
* @return boolean The result of the clear operation. |
43
|
|
|
* |
44
|
|
|
* @since 1.0 |
45
|
|
|
*/ |
46
|
|
|
public function clear() |
47
|
|
|
{ |
48
|
|
|
$this->connect(); |
49
|
|
|
|
50
|
|
|
return $this->driver->flushDB(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Method to get a storage entry value from a key. |
55
|
|
|
* |
56
|
|
|
* @param string $key The storage entry identifier. |
57
|
|
|
* |
58
|
|
|
* @return CacheItemInterface |
59
|
|
|
* |
60
|
|
|
* @since 1.0 |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function getItem($key) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->connect(); |
65
|
|
|
|
66
|
|
|
$value = $this->driver->get($key); |
67
|
|
|
$item = new Item($key); |
68
|
|
|
|
69
|
|
|
if ($value !== false) |
70
|
|
|
{ |
71
|
|
|
$item->set($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $item; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Method to remove a storage entry for a key. |
79
|
|
|
* |
80
|
|
|
* @param string $key The storage entry identifier. |
81
|
|
|
* |
82
|
|
|
* @return boolean |
83
|
|
|
* |
84
|
|
|
* @since 1.0 |
85
|
|
|
*/ |
86
|
|
|
public function deleteItem($key) |
87
|
|
|
{ |
88
|
|
|
$this->connect(); |
89
|
|
|
|
90
|
|
|
if ($this->hasItem($key)) |
91
|
|
|
{ |
92
|
|
|
return (bool) $this->driver->del($key); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// If the item doesn't exist, no error |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Persists a cache item immediately. |
101
|
|
|
* |
102
|
|
|
* @param CacheItemInterface $item The cache item to save. |
103
|
|
|
* |
104
|
|
|
* @return bool True if the item was successfully persisted. False if there was an error. |
105
|
|
|
*/ |
106
|
|
|
public function save(CacheItemInterface $item) |
107
|
|
|
{ |
108
|
|
|
$this->connect(); |
109
|
|
|
|
110
|
|
|
if ($item instanceof HasExpirationDateInterface) |
111
|
|
|
{ |
112
|
|
|
$ttl = $this->convertItemExpiryToSeconds($item); |
113
|
|
|
|
114
|
|
|
if ($ttl > 0) |
115
|
|
|
{ |
116
|
|
|
return $this->driver->setex($item->getKey(), $ttl, $item->get()); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->driver->set($item->getKey(), $item->get()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Method to determine whether a storage entry has been set for a key. |
125
|
|
|
* |
126
|
|
|
* @param string $key The storage entry identifier. |
127
|
|
|
* |
128
|
|
|
* @return boolean |
129
|
|
|
* |
130
|
|
|
* @since 1.0 |
131
|
|
|
*/ |
132
|
|
|
public function hasItem($key) |
133
|
|
|
{ |
134
|
|
|
$this->connect(); |
135
|
|
|
|
136
|
|
|
return $this->driver->exists($key); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Test to see if the CacheItemPoolInterface is available |
141
|
|
|
* |
142
|
|
|
* @return boolean True on success, false otherwise |
143
|
|
|
* |
144
|
|
|
* @since __DEPLOY_VERSION__ |
145
|
|
|
*/ |
146
|
10 |
|
public static function isSupported() |
147
|
|
|
{ |
148
|
10 |
|
return (extension_loaded('redis') && class_exists('Redis')); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Connect to the Redis servers if the connection does not already exist. |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
* |
156
|
|
|
* @since 1.0 |
157
|
|
|
*/ |
158
|
|
|
private function connect() |
159
|
|
|
{ |
160
|
|
|
// We want to only create the driver once. |
161
|
|
|
if (isset($this->driver)) |
162
|
|
|
{ |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$host = isset($this->options['redis.host'])? $this->options['redis.host'] : self::REDIS_HOST; |
167
|
|
|
$port = isset($this->options['redis.port'])? $this->options['redis.port'] : self::REDIS_PORT; |
168
|
|
|
|
169
|
|
|
$this->driver = new RedisDriver; |
170
|
|
|
|
171
|
|
|
if (($host == 'localhost' || filter_var($host, FILTER_VALIDATE_IP))) |
172
|
|
|
{ |
173
|
|
|
$this->driver->connect('tcp://' . $host . ':' . $port, $port); |
174
|
|
|
} |
175
|
|
|
else |
176
|
|
|
{ |
177
|
|
|
$this->driver->connect($host, null); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.