1
|
|
|
<?php |
2
|
|
|
namespace nzedb\libraries; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Cache |
6
|
|
|
* |
7
|
|
|
* Class for connecting to a memcached or redis server to cache data. |
8
|
|
|
* |
9
|
|
|
* @package nzedb\libraries |
10
|
|
|
*/ |
11
|
|
|
class Cache |
12
|
|
|
{ |
13
|
|
|
const SERIALIZER_PHP = 0; |
14
|
|
|
const SERIALIZER_IGBINARY = 1; |
15
|
|
|
const SERIALIZER_NONE = 2; |
16
|
|
|
|
17
|
|
|
const TYPE_DISABLED = 0; |
18
|
|
|
const TYPE_MEMCACHED = 1; |
19
|
|
|
const TYPE_REDIS = 2; |
20
|
|
|
const TYPE_APC = 3; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Memcached|\Redis |
24
|
|
|
*/ |
25
|
|
|
private $server = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Are we connected to the cache server? |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $connected = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Optional socket file location. |
35
|
|
|
* @var bool|string |
36
|
|
|
*/ |
37
|
|
|
private $socketFile; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Store data on the cache server. |
41
|
|
|
* |
42
|
|
|
* @param string $key Key we can use to retrieve the data. |
43
|
|
|
* @param string|array $data Data to store on the cache server. |
44
|
|
|
* @param int $expiration Time before the data expires on the cache server. |
45
|
|
|
* |
46
|
|
|
* @return bool Success/Failure. |
47
|
|
|
* @access public |
48
|
|
|
*/ |
49
|
|
|
public function set($key, $data, $expiration) |
50
|
|
|
{ |
51
|
|
|
if ($this->ping()) { |
52
|
|
|
switch (nZEDb_CACHE_TYPE) { |
53
|
|
|
case self::TYPE_REDIS: |
54
|
|
|
case self::TYPE_MEMCACHED: |
55
|
|
|
return $this->server->set($key, $data, $expiration); |
56
|
|
|
case self::TYPE_APC: |
57
|
|
|
return apc_add($key, $data, $expiration); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Attempt to retrieve a value from the cache server, if not set it. |
65
|
|
|
* |
66
|
|
|
* @param string $key Key we can use to retrieve the data. |
67
|
|
|
* |
68
|
|
|
* @return bool|string False on failure or String, data belonging to the key. |
69
|
|
|
* @access public |
70
|
|
|
*/ |
71
|
|
|
public function get($key) |
72
|
|
|
{ |
73
|
|
|
if ($this->ping()) { |
74
|
|
|
$data = ''; |
75
|
|
|
switch (nZEDb_CACHE_TYPE) { |
76
|
|
|
case self::TYPE_REDIS: |
77
|
|
|
case self::TYPE_MEMCACHED: |
78
|
|
|
$data = $this->server->get($key); |
79
|
|
|
break; |
80
|
|
|
case self::TYPE_APC: |
81
|
|
|
$data = apc_fetch($key); |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
return $data; |
85
|
|
|
} |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Delete data tied to a key on the cache server. |
91
|
|
|
* |
92
|
|
|
* @param string $key Key we can use to retrieve the data. |
93
|
|
|
* |
94
|
|
|
* @return bool True if deleted, false if not. |
95
|
|
|
* @access public |
96
|
|
|
*/ |
97
|
|
|
public function delete($key) |
98
|
|
|
{ |
99
|
|
|
if ($this->ping()) { |
100
|
|
|
switch (nZEDb_CACHE_TYPE) { |
101
|
|
|
case self::TYPE_REDIS: |
102
|
|
|
case self::TYPE_MEMCACHED: |
103
|
|
|
return (bool)$this->server->delete($key); |
104
|
|
|
case self::TYPE_APC: |
105
|
|
|
return apc_delete($key); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Flush all data from the cache server? |
113
|
|
|
*/ |
114
|
|
|
public function flush() |
115
|
|
|
{ |
116
|
|
|
if ($this->ping()) { |
117
|
|
|
switch (nZEDb_CACHE_TYPE) { |
118
|
|
|
case self::TYPE_REDIS: |
119
|
|
|
$this->server->flushAll(); |
120
|
|
|
break; |
121
|
|
|
case self::TYPE_MEMCACHED: |
122
|
|
|
$this->server->flush(); |
123
|
|
|
break; |
124
|
|
|
case self::TYPE_APC: |
125
|
|
|
apc_clear_cache("user"); |
126
|
|
|
apc_clear_cache(); |
127
|
|
|
break; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create a SHA1 hash from a string which can be used to store/retrieve data. |
134
|
|
|
* |
135
|
|
|
* @param string $string |
136
|
|
|
* |
137
|
|
|
* @return string SHA1 hash of the input string. |
138
|
|
|
* @access public |
139
|
|
|
*/ |
140
|
|
|
public function createKey($string) |
141
|
|
|
{ |
142
|
|
|
return sha1($string); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get cache server statistics. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
* @access public |
150
|
|
|
*/ |
151
|
|
|
public function serverStatistics() |
152
|
|
|
{ |
153
|
|
|
if ($this->ping()) { |
154
|
|
|
switch (nZEDb_CACHE_TYPE) { |
155
|
|
|
case self::TYPE_REDIS: |
156
|
|
|
return $this->server->info(); |
157
|
|
|
case self::TYPE_MEMCACHED: |
158
|
|
|
return $this->server->getStats(); |
159
|
|
|
case self::TYPE_APC: |
160
|
|
|
return apc_cache_info(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
return array(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Verify the user's cache settings, try to connect to the cache server. |
168
|
|
|
*/ |
169
|
|
|
public function __construct() |
170
|
|
|
{ |
171
|
|
|
if (!defined('nZEDb_CACHE_HOSTS')) { |
172
|
|
|
throw new CacheException( |
173
|
|
|
'The nZEDb_CACHE_HOSTS is not defined! Define it in settings.php' |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (!defined('nZEDb_CACHE_TIMEOUT')) { |
178
|
|
|
throw new CacheException( |
179
|
|
|
'The nZEDb_CACHE_TIMEOUT is not defined! Define it in settings.php, it is the time in seconds to time out from your cache server.' |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->socketFile = false; |
184
|
|
|
if (defined('nZEDb_CACHE_SOCKET_FILE') && nZEDb_CACHE_SOCKET_FILE != '') { |
|
|
|
|
185
|
|
|
$this->socketFile = true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$serializer = false; |
189
|
|
|
if (defined('nZEDb_CACHE_SERIALIZER')) { |
190
|
|
|
$serializer = true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
switch (nZEDb_CACHE_TYPE) { |
194
|
|
|
|
195
|
|
|
case self::TYPE_REDIS: |
196
|
|
|
if (!extension_loaded('redis')) { |
197
|
|
|
throw new CacheException('The redis extension is not loaded!'); |
198
|
|
|
} |
199
|
|
|
$this->server = new \Redis(); |
200
|
|
|
$this->connect(); |
201
|
|
|
if ($serializer) { |
202
|
|
|
$this->server->setOption(\Redis::OPT_SERIALIZER, $this->verifySerializer()); |
203
|
|
|
} |
204
|
|
|
break; |
205
|
|
|
|
206
|
|
|
case self::TYPE_MEMCACHED: |
207
|
|
|
if (!extension_loaded('memcached')) { |
208
|
|
|
throw new CacheException('The memcached extension is not loaded!'); |
209
|
|
|
} |
210
|
|
|
$this->server = new \Memcached(); |
211
|
|
|
if ($serializer) { |
212
|
|
|
$this->server->setOption(\Memcached::OPT_SERIALIZER, $this->verifySerializer()); |
213
|
|
|
} |
214
|
|
|
$this->server->setOption(\Memcached::OPT_COMPRESSION, (defined('nZEDb_CACHE_COMPRESSION') ? nZEDb_CACHE_COMPRESSION : false)); |
215
|
|
|
$this->connect(); |
216
|
|
|
break; |
217
|
|
|
|
218
|
|
|
case self::TYPE_APC: |
219
|
|
|
// Faster than checking if apcu or apc is loaded. |
220
|
|
|
if (!function_exists('apc_add')) { |
221
|
|
|
throw new CacheException('The APCu extension is not loaded or enabled!'); |
222
|
|
|
} |
223
|
|
|
$this->connect(); |
224
|
|
|
break; |
225
|
|
|
|
226
|
|
|
case self::TYPE_DISABLED: |
227
|
|
|
default: |
228
|
|
|
break; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Destroy the connections. |
234
|
|
|
*/ |
235
|
|
|
public function __destruct() |
236
|
|
|
{ |
237
|
|
|
switch (nZEDb_CACHE_TYPE) { |
238
|
|
|
case self::TYPE_REDIS: |
239
|
|
|
$this->server->close(); |
240
|
|
|
break; |
241
|
|
|
case self::TYPE_MEMCACHED: |
242
|
|
|
$this->server->quit(); |
243
|
|
|
break; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Connect to the cache server(s). |
249
|
|
|
* |
250
|
|
|
* @throws CacheException |
251
|
|
|
* @access private |
252
|
|
|
*/ |
253
|
|
|
private function connect() |
254
|
|
|
{ |
255
|
|
|
$this->connected = false; |
256
|
|
|
switch (nZEDb_CACHE_TYPE) { |
257
|
|
|
case self::TYPE_REDIS: |
258
|
|
|
if ($this->socketFile === false) { |
259
|
|
|
$servers = unserialize(nZEDb_CACHE_HOSTS); |
260
|
|
|
foreach ($servers as $server) { |
261
|
|
|
if ($this->server->connect($server['host'], $server['port'], (float)nZEDb_CACHE_TIMEOUT) === false) { |
262
|
|
|
throw new CacheException('Error connecting to the Redis server!'); |
263
|
|
|
} else { |
264
|
|
|
$this->connected = true; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} else { |
268
|
|
|
if ($this->server->connect(nZEDb_CACHE_SOCKET_FILE) === false) { |
269
|
|
|
throw new CacheException('Error connecting to the Redis server!'); |
270
|
|
|
} else { |
271
|
|
|
$this->connected = true; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
break; |
275
|
|
|
case self::TYPE_MEMCACHED: |
276
|
|
|
$params = ($this->socketFile === false ? unserialize(nZEDb_CACHE_HOSTS) : [[nZEDb_CACHE_SOCKET_FILE, 'port' => 0]]); |
277
|
|
|
if ($this->server->addServers($params) === false) { |
278
|
|
|
throw new CacheException('Error connecting to the Memcached server!'); |
279
|
|
|
} else { |
280
|
|
|
$this->connected = true; |
281
|
|
|
} |
282
|
|
|
break; |
283
|
|
|
case self::TYPE_APC: |
284
|
|
|
$this->connected = true; |
285
|
|
|
break; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Check if we are still connected to the cache server, reconnect if not. |
291
|
|
|
* |
292
|
|
|
* @return bool |
293
|
|
|
*/ |
294
|
|
|
private function ping() |
295
|
|
|
{ |
296
|
|
|
if (!$this->connected) { |
297
|
|
|
return false; |
298
|
|
|
} |
299
|
|
|
switch (nZEDb_CACHE_TYPE) { |
300
|
|
|
case self::TYPE_REDIS: |
301
|
|
|
try { |
302
|
|
|
return (bool)$this->server->ping(); |
303
|
|
|
} catch (\RedisException $error) { |
304
|
|
|
// nothing to see here, move along |
305
|
|
|
} |
306
|
|
|
break; |
307
|
|
|
case self::TYPE_MEMCACHED: |
308
|
|
|
$versions = $this->server->getVersion(); |
309
|
|
|
if ($versions) { |
310
|
|
|
foreach ($versions as $version) { |
311
|
|
|
if ($version != "255.255.255") { |
312
|
|
|
return true; |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
break; |
317
|
|
|
case self::TYPE_APC: |
318
|
|
|
return true; |
319
|
|
|
default: |
320
|
|
|
return false; |
321
|
|
|
} |
322
|
|
|
$this->connect(); |
323
|
|
|
return $this->connected; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Verify the user selected serializer, return the memcached or redis appropriate serializer option. |
328
|
|
|
* |
329
|
|
|
* @return int |
330
|
|
|
* @throws CacheException |
331
|
|
|
* @access private |
332
|
|
|
*/ |
333
|
|
|
private function verifySerializer() |
334
|
|
|
{ |
335
|
|
|
switch (nZEDb_CACHE_SERIALIZER) { |
336
|
|
|
case self::SERIALIZER_IGBINARY: |
337
|
|
|
if (!extension_loaded('igbinary')) { |
338
|
|
|
throw new CacheException('Error: The igbinary extension is not loaded!'); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
switch (nZEDb_CACHE_TYPE) { |
342
|
|
|
case self::TYPE_REDIS: |
343
|
|
|
// If this is not defined, it means phpredis was not compiled with --enable-redis-igbinary |
344
|
|
|
if (!defined('\Redis::SERIALIZER_IGBINARY')) { |
345
|
|
|
throw new CacheException('Error: phpredis was not compiled with igbinary support!'); |
346
|
|
|
} |
347
|
|
|
return \Redis::SERIALIZER_IGBINARY; |
348
|
|
|
case self::TYPE_MEMCACHED: |
349
|
|
|
if (\Memcached::HAVE_IGBINARY > 0) { |
350
|
|
|
return \Memcached::SERIALIZER_IGBINARY; |
351
|
|
|
} |
352
|
|
|
throw new CacheException('Error: You have not compiled Memcached with igbinary support!'); |
353
|
|
|
case self::TYPE_APC: // Ignore - set by apc.serializer setting. |
354
|
|
|
default: |
355
|
|
|
return null; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
case self::SERIALIZER_NONE: |
359
|
|
|
// Only redis supports this. |
360
|
|
|
if (nZEDb_CACHE_TYPE != self::TYPE_REDIS) { |
361
|
|
|
throw new CacheException('Error: Disabled serialization is only available on Redis!'); |
362
|
|
|
} |
363
|
|
|
return \Redis::SERIALIZER_NONE; |
364
|
|
|
|
365
|
|
|
case self::SERIALIZER_PHP: |
366
|
|
|
default: |
367
|
|
|
switch (nZEDb_CACHE_TYPE) { |
368
|
|
|
case self::TYPE_REDIS: |
369
|
|
|
return \Redis::SERIALIZER_PHP; |
370
|
|
|
case self::TYPE_MEMCACHED: |
371
|
|
|
return \Memcached::SERIALIZER_PHP; |
372
|
|
|
default: |
373
|
|
|
return null; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
} |
379
|
|
|
|