|
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\Adapter; |
|
10
|
|
|
|
|
11
|
|
|
use Joomla\Cache\AbstractCacheItemPool; |
|
12
|
|
|
use Joomla\Cache\Exception\RuntimeException; |
|
13
|
|
|
use Joomla\Cache\Item\HasExpirationDateInterface; |
|
14
|
|
|
use Joomla\Cache\Item\Item; |
|
15
|
|
|
use Psr\Cache\CacheItemInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Memcached cache driver for the Joomla Framework. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Memcached extends AbstractCacheItemPool |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* The Memcached driver |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Memcached |
|
28
|
|
|
* @since 1.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
private $driver; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Memcached $memcached The Memcached driver being used for this pool |
|
36
|
|
|
* @param array|\ArrayAccess $options An options array, or an object that implements \ArrayAccess |
|
37
|
|
|
* |
|
38
|
|
|
* @since 1.0 |
|
39
|
|
|
*/ |
|
40
|
12 |
|
public function __construct(\Memcached $memcached, $options = []) |
|
41
|
|
|
{ |
|
42
|
|
|
// Parent sets up the caching options and checks their type |
|
43
|
12 |
|
parent::__construct($options); |
|
44
|
|
|
|
|
45
|
12 |
|
$this->driver = $memcached; |
|
46
|
12 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* This will wipe out the entire cache's keys |
|
50
|
|
|
* |
|
51
|
|
|
* @return boolean The result of the clear operation. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 1.0 |
|
54
|
|
|
*/ |
|
55
|
12 |
|
public function clear() |
|
56
|
|
|
{ |
|
57
|
12 |
|
return $this->driver->flush(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Method to get a storage entry value from a key. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $key The storage entry identifier. |
|
64
|
|
|
* |
|
65
|
|
|
* @return CacheItemInterface |
|
66
|
|
|
* |
|
67
|
|
|
* @since 1.0 |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function getItem($key) |
|
70
|
|
|
{ |
|
71
|
4 |
|
$value = $this->driver->get($key); |
|
72
|
4 |
|
$code = $this->driver->getResultCode(); |
|
73
|
4 |
|
$item = new Item($key); |
|
74
|
|
|
|
|
75
|
4 |
|
if ($code === \Memcached::RES_SUCCESS) |
|
76
|
4 |
|
{ |
|
77
|
3 |
|
$item->set($value); |
|
78
|
3 |
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
return $item; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns a traversable set of cache items. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $keys A list of keys that can obtained in a single operation. |
|
87
|
|
|
* |
|
88
|
|
|
* @return CacheItemInterface[] An associative array of CacheItemInterface objects keyed on the cache key. |
|
89
|
|
|
* |
|
90
|
|
|
* @since __DEPLOY_VERSION__ |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getItems(array $keys = []) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$data = $this->driver->getMulti($keys); |
|
95
|
|
|
|
|
96
|
1 |
|
$result = []; |
|
97
|
|
|
|
|
98
|
1 |
|
foreach ($keys as $key) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$item = new Item($key); |
|
101
|
|
|
|
|
102
|
1 |
|
if (array_key_exists($key, $data)) |
|
103
|
1 |
|
{ |
|
104
|
1 |
|
$item->set($data[$key]); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
$result[$key] = $item; |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
return $result; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Method to remove a storage entry for a key. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $key The storage entry identifier. |
|
117
|
|
|
* |
|
118
|
|
|
* @return boolean |
|
119
|
|
|
* |
|
120
|
|
|
* @since 1.0 |
|
121
|
|
|
* @throws RuntimeException |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function deleteItem($key) |
|
124
|
|
|
{ |
|
125
|
1 |
|
if ($this->hasItem($key)) |
|
126
|
1 |
|
{ |
|
127
|
1 |
|
$this->driver->delete($key); |
|
128
|
|
|
|
|
129
|
1 |
|
$rc = $this->driver->getResultCode(); |
|
130
|
|
|
|
|
131
|
|
|
// If the item was not successfully removed nor did not exist then raise an error |
|
132
|
1 |
|
if (($rc !== \Memcached::RES_SUCCESS)) |
|
133
|
1 |
|
{ |
|
134
|
|
|
throw new RuntimeException(sprintf('Unable to remove cache entry for %s. Error message `%s`.', $key, $this->driver->getResultMessage())); |
|
135
|
|
|
} |
|
136
|
1 |
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Removes multiple items from the pool. |
|
143
|
|
|
* |
|
144
|
|
|
* @param array $keys An array of keys that should be removed from the pool. |
|
145
|
|
|
* |
|
146
|
|
|
* @return boolean |
|
147
|
|
|
* |
|
148
|
|
|
* @since __DEPLOY_VERSION__ |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function deleteItems(array $keys) |
|
151
|
|
|
{ |
|
152
|
|
|
// HHVM doesn't support deleteMulti |
|
153
|
1 |
|
if (!method_exists($this->driver, 'deleteMulti')) |
|
154
|
1 |
|
{ |
|
155
|
|
|
return parent::deleteItems($keys); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
$deleted = $this->driver->deleteMulti($keys); |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
1 |
|
foreach ($deleted as $key => $value) |
|
161
|
|
|
{ |
|
162
|
|
|
/* |
|
163
|
|
|
* The return of deleteMulti is not consistent with the documentation for error cases, |
|
164
|
|
|
* so check for an explicit boolean true for successful deletion |
|
165
|
|
|
*/ |
|
166
|
1 |
|
if ($value !== true && $value !== \Memcached::RES_NOTFOUND) |
|
167
|
1 |
|
{ |
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
1 |
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
return true; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Persists a cache item immediately. |
|
177
|
|
|
* |
|
178
|
|
|
* @param CacheItemInterface $item The cache item to save. |
|
179
|
|
|
* |
|
180
|
|
|
* @return boolean |
|
181
|
|
|
* |
|
182
|
|
|
* @since __DEPLOY_VERSION__ |
|
183
|
|
|
*/ |
|
184
|
9 |
|
public function save(CacheItemInterface $item) |
|
185
|
|
|
{ |
|
186
|
9 |
|
if ($item instanceof HasExpirationDateInterface) |
|
187
|
9 |
|
{ |
|
188
|
1 |
|
$ttl = $this->convertItemExpiryToSeconds($item); |
|
189
|
1 |
|
} |
|
190
|
|
|
else |
|
191
|
|
|
{ |
|
192
|
8 |
|
$ttl = 0; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
9 |
|
$this->driver->set($item->getKey(), $item->get(), $ttl); |
|
196
|
|
|
|
|
197
|
9 |
|
return (bool) ($this->driver->getResultCode() == \Memcached::RES_SUCCESS); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Method to determine whether a storage entry has been set for a key. |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $key The storage entry identifier. |
|
204
|
|
|
* |
|
205
|
|
|
* @return boolean |
|
206
|
|
|
* |
|
207
|
|
|
* @since 1.0 |
|
208
|
|
|
*/ |
|
209
|
4 |
|
public function hasItem($key) |
|
210
|
|
|
{ |
|
211
|
4 |
|
$this->driver->get($key); |
|
212
|
|
|
|
|
213
|
4 |
|
return ($this->driver->getResultCode() !== \Memcached::RES_NOTFOUND); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Test to see if the CacheItemPoolInterface is available |
|
218
|
|
|
* |
|
219
|
|
|
* @return boolean True on success, false otherwise |
|
220
|
|
|
* |
|
221
|
|
|
* @since __DEPLOY_VERSION__ |
|
222
|
|
|
*/ |
|
223
|
12 |
|
public static function isSupported() |
|
224
|
|
|
{ |
|
225
|
|
|
/* |
|
226
|
|
|
* GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded. |
|
227
|
|
|
* If the class is there, we can assume it works. |
|
228
|
|
|
*/ |
|
229
|
12 |
|
return (class_exists('Memcached')); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.