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