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\Item\HasExpirationDateInterface; |
13
|
|
|
use Joomla\Cache\Item\Item; |
14
|
|
|
use Psr\Cache\CacheItemInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* APCu cache driver for the Joomla Framework. |
18
|
|
|
* |
19
|
|
|
* @since __DEPLOY_VERSION__ |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class Apcu extends AbstractCacheItemPool |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* This will wipe out the entire cache's keys |
25
|
|
|
* |
26
|
|
|
* @return boolean The result of the clear operation. |
27
|
|
|
* |
28
|
|
|
* @since __DEPLOY_VERSION__ |
29
|
|
|
*/ |
30
|
11 |
|
public function clear() |
31
|
|
|
{ |
32
|
11 |
|
return apcu_clear_cache(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Method to get a storage entry value from a key. |
37
|
|
|
* |
38
|
|
|
* @param string $key The storage entry identifier. |
39
|
|
|
* |
40
|
|
|
* @return CacheItemInterface |
41
|
|
|
* |
42
|
|
|
* @since __DEPLOY_VERSION__ |
43
|
|
|
* @throws \RuntimeException |
44
|
|
|
*/ |
45
|
3 |
|
public function getItem($key) |
46
|
|
|
{ |
47
|
3 |
|
$success = false; |
48
|
3 |
|
$value = apcu_fetch($key, $success); |
49
|
3 |
|
$item = new Item($key); |
50
|
|
|
|
51
|
|
|
if ($success) |
52
|
3 |
|
{ |
53
|
3 |
|
$item->set($value); |
54
|
3 |
|
} |
55
|
|
|
|
56
|
3 |
|
return $item; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Obtain multiple CacheItems by their unique keys. |
61
|
|
|
* |
62
|
|
|
* @param array $keys A list of keys that can obtained in a single operation. |
63
|
|
|
* |
64
|
|
|
* @return array An associative array of CacheItem objects keyed on the cache key. |
65
|
|
|
* |
66
|
|
|
* @since __DEPLOY_VERSION__ |
67
|
|
|
*/ |
68
|
1 |
|
public function getItems(array $keys = array()) |
69
|
|
|
{ |
70
|
1 |
|
$items = array(); |
71
|
1 |
|
$success = false; |
72
|
1 |
|
$values = apcu_fetch($keys, $success); |
73
|
|
|
|
74
|
1 |
|
if ($success && is_array($values)) |
75
|
1 |
|
{ |
76
|
1 |
|
foreach ($keys as $key) |
77
|
|
|
{ |
78
|
1 |
|
$items[$key] = new Item($key); |
79
|
|
|
|
80
|
1 |
|
if (isset($values[$key])) |
81
|
1 |
|
{ |
82
|
1 |
|
$items[$key]->set($values[$key]); |
83
|
1 |
|
} |
84
|
1 |
|
} |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
return $items; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Method to remove a storage entry for a key. |
92
|
|
|
* |
93
|
|
|
* @param string $key The storage entry identifier. |
94
|
|
|
* |
95
|
|
|
* @return boolean |
96
|
|
|
* |
97
|
|
|
* @since __DEPLOY_VERSION__ |
98
|
|
|
*/ |
99
|
2 |
|
public function deleteItem($key) |
100
|
|
|
{ |
101
|
2 |
|
if ($this->hasItem($key)) |
102
|
2 |
|
{ |
103
|
2 |
|
return apcu_delete($key); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// If the item doesn't exist, no error |
107
|
2 |
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Persists a cache item immediately. |
112
|
|
|
* |
113
|
|
|
* @param CacheItemInterface $item The cache item to save. |
114
|
|
|
* |
115
|
|
|
* @return static |
116
|
|
|
* The invoked object. |
117
|
|
|
*/ |
118
|
8 |
|
public function save(CacheItemInterface $item) |
119
|
|
|
{ |
120
|
|
|
// If we are able to find out when the item expires - find out. Else bail. |
121
|
8 |
|
if ($item instanceof HasExpirationDateInterface) |
122
|
8 |
|
{ |
123
|
|
|
$ttl = $this->convertItemExpiryToSeconds($item); |
124
|
|
|
} |
125
|
|
|
else |
126
|
|
|
{ |
127
|
8 |
|
$ttl = 0; |
128
|
|
|
} |
129
|
|
|
|
130
|
8 |
|
return apcu_store($item->getKey(), $item->get(), $ttl); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Method to determine whether a storage entry has been set for a key. |
135
|
|
|
* |
136
|
|
|
* @param string $key The storage entry identifier. |
137
|
|
|
* |
138
|
|
|
* @return boolean |
139
|
|
|
* |
140
|
|
|
* @since __DEPLOY_VERSION__ |
141
|
|
|
*/ |
142
|
5 |
|
public function hasItem($key) |
143
|
|
|
{ |
144
|
5 |
|
return apcu_exists($key); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Test to see if the CacheItemPoolInterface is available |
149
|
|
|
* |
150
|
|
|
* @return boolean True on success, false otherwise |
151
|
|
|
* |
152
|
|
|
* @since __DEPLOY_VERSION__ |
153
|
|
|
*/ |
154
|
11 |
|
public static function isSupported() |
155
|
|
|
{ |
156
|
11 |
|
$supported = extension_loaded('apcu') && ini_get('apc.enabled'); |
157
|
|
|
|
158
|
|
|
// If on the CLI interface, the `apc.enable_cli` option must also be enabled |
159
|
11 |
|
if ($supported && php_sapi_name() === 'cli') |
160
|
11 |
|
{ |
161
|
11 |
|
$supported = ini_get('apc.enable_cli'); |
162
|
11 |
|
} |
163
|
|
|
|
164
|
11 |
|
return (bool) $supported; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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.