1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Cache\Adapters\Apcu; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Cache\Item; |
||
19 | use Psr\Cache\CacheItemInterface; |
||
0 ignored issues
–
show
|
|||
20 | use O2System\Spl\Exceptions\Logic\InvalidArgumentException; |
||
21 | |||
22 | /** |
||
23 | * Class ItemPool |
||
24 | * |
||
25 | * @package O2System\Cache\Adapters\Apc |
||
26 | */ |
||
27 | class ItemPool extends Adapter |
||
28 | { |
||
29 | /** |
||
30 | * ItemPool::getItems |
||
31 | * |
||
32 | * Returns a traversable set of cache items. |
||
33 | * |
||
34 | * @param string[] $keys |
||
35 | * An indexed array of keys of items to retrieve. |
||
36 | * |
||
37 | * @throws InvalidArgumentException |
||
38 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
39 | * MUST be thrown. |
||
40 | * |
||
41 | * @return array|\Traversable |
||
42 | * A traversable collection of Cache Items keyed by the cache keys of |
||
43 | * each item. A Cache item will be returned for each key, even if that |
||
44 | * key is not found. However, if no keys are specified then an empty |
||
45 | * traversable MUST be returned instead. |
||
46 | */ |
||
47 | public function getItems(array $keys = []) |
||
48 | { |
||
49 | if ( ! is_array($keys)) { |
||
0 ignored issues
–
show
|
|||
50 | throw new InvalidArgumentException('E_INVALID_ARGUMENT_ARRAY_CACHE_EXCEPTION'); |
||
51 | } |
||
52 | $items = []; |
||
53 | |||
54 | if (empty($keys) AND class_exists('APCUIterator', false)) { |
||
55 | $apcIterator = new \APCUIterator(); |
||
56 | |||
57 | foreach ($apcIterator as $item) { |
||
58 | $items[] = new Item(str_replace($this->prefixKey, '', $item[ 'key' ]), $item[ 'value' ]); |
||
59 | } |
||
60 | } elseif (count($keys)) { |
||
61 | foreach ($keys as $key) { |
||
62 | $items[] = $this->getItem($key); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | return $items; |
||
67 | } |
||
68 | |||
69 | // ------------------------------------------------------------------------ |
||
70 | |||
71 | /** |
||
72 | * ItemPool::getKey |
||
73 | * |
||
74 | * Returns a Cache Item representing the specified key. |
||
75 | * |
||
76 | * This method must always return a CacheItemInterface object, even in case of |
||
77 | * a cache miss. It MUST NOT return null. |
||
78 | * |
||
79 | * @param string $key |
||
80 | * The key for which to return the corresponding Cache Item. |
||
81 | * |
||
82 | * @throws InvalidArgumentException |
||
83 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
84 | * MUST be thrown. |
||
85 | * |
||
86 | * @return CacheItemInterface |
||
87 | * The corresponding Cache Item. |
||
88 | */ |
||
89 | public function getItem($key) |
||
90 | { |
||
91 | if ( ! is_string($key)) { |
||
0 ignored issues
–
show
|
|||
92 | throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION'); |
||
93 | } |
||
94 | |||
95 | $success = false; |
||
96 | |||
97 | $metadata = apcu_fetch($this->prefixKey . $key, $success); |
||
98 | |||
99 | return new Item($key, $metadata); |
||
100 | } |
||
101 | |||
102 | // ------------------------------------------------------------------------ |
||
103 | |||
104 | /** |
||
105 | * ItemPool::hasItem |
||
106 | * |
||
107 | * Confirms if the cache contains specified cache item. |
||
108 | * |
||
109 | * Note: This method MAY avoid retrieving the cached value for performance reasons. |
||
110 | * This could result in a race condition with CacheItemInterface::get(). To avoid |
||
111 | * such situation use CacheItemInterface::isHit() instead. |
||
112 | * |
||
113 | * @param string $key |
||
114 | * The key for which to check existence. |
||
115 | * |
||
116 | * @throws InvalidArgumentException |
||
117 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
118 | * MUST be thrown. |
||
119 | * |
||
120 | * @return bool |
||
121 | * True if item exists in the cache, false otherwise. |
||
122 | */ |
||
123 | public function hasItem($key) |
||
124 | { |
||
125 | if ( ! is_string($key)) { |
||
0 ignored issues
–
show
|
|||
126 | throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION'); |
||
127 | } |
||
128 | |||
129 | return (bool)apcu_exists($this->prefixKey . $key); |
||
130 | } |
||
131 | |||
132 | // ------------------------------------------------------------------------ |
||
133 | |||
134 | /** |
||
135 | * ItemPool::clear |
||
136 | * |
||
137 | * Deletes all items in the pool. |
||
138 | * |
||
139 | * @return bool |
||
140 | * True if the pool was successfully cleared. False if there was an error. |
||
141 | */ |
||
142 | public function clear() |
||
143 | { |
||
144 | return apcu_clear_cache(); |
||
145 | } |
||
146 | |||
147 | // ------------------------------------------------------------------------ |
||
148 | |||
149 | /** |
||
150 | * ItemPool::deleteItem |
||
151 | * |
||
152 | * Removes the item from the pool. |
||
153 | * |
||
154 | * @param string $key |
||
155 | * The key to delete. |
||
156 | * |
||
157 | * @throws InvalidArgumentException |
||
158 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
159 | * MUST be thrown. |
||
160 | * |
||
161 | * @return bool |
||
162 | * True if the item was successfully removed. False if there was an error. |
||
163 | */ |
||
164 | public function deleteItem($key) |
||
165 | { |
||
166 | if ( ! is_string($key)) { |
||
0 ignored issues
–
show
|
|||
167 | throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION'); |
||
168 | } |
||
169 | |||
170 | return apcu_delete($this->prefixKey . $key); |
||
0 ignored issues
–
show
|
|||
171 | } |
||
172 | |||
173 | // ------------------------------------------------------------------------ |
||
174 | |||
175 | /** |
||
176 | * ItemPool::save |
||
177 | * |
||
178 | * Persists a cache item immediately. |
||
179 | * |
||
180 | * @param CacheItemInterface $item |
||
181 | * The cache item to save. |
||
182 | * |
||
183 | * @return bool |
||
184 | * True if the item was successfully persisted. False if there was an error. |
||
185 | */ |
||
186 | public function save(CacheItemInterface $item) |
||
187 | { |
||
188 | $metadata = $item->getMetadata(); |
||
189 | $metadata[ 'data' ] = $item->get(); |
||
190 | |||
191 | return apcu_store($this->prefixKey . $item->getKey(), $metadata, $metadata[ 'ttl' ]); |
||
0 ignored issues
–
show
|
|||
192 | } |
||
193 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths