|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
*TechDivision\Import\Cache\ArrayCache |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cache; |
|
22
|
|
|
|
|
23
|
|
|
use Psr\Cache\CacheItemInterface; |
|
24
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* A PSR-6 compatible cache implementation. |
|
28
|
|
|
* |
|
29
|
|
|
* ```php |
|
30
|
|
|
* // create a new item by trying to get it from the cache |
|
31
|
|
|
* $accessToken = $arrayCache->getItem('access_token'); |
|
32
|
|
|
* |
|
33
|
|
|
* // assign a value to the item and save it |
|
34
|
|
|
* $accessToken->set('YWraeE4Esro5IsncUEM2amwyh4dS5IzL'); |
|
35
|
|
|
* $arrayCache->save($accessToken); |
|
36
|
|
|
* |
|
37
|
|
|
* // retrieve the cache item |
|
38
|
|
|
* $cacheItem = $arrayCache->getItem('access_token'); |
|
39
|
|
|
* if (!cacheItem->isHit()) { |
|
40
|
|
|
* // ... item does not exists in the cache |
|
41
|
|
|
* } |
|
42
|
|
|
* // retrieve the value stored by the item |
|
43
|
|
|
* $accessToken = cacheItem->get(); |
|
44
|
|
|
* |
|
45
|
|
|
* // remove the cache item |
|
46
|
|
|
* $arrayCache->deleteItem('access_token'); |
|
47
|
|
|
* ``` |
|
48
|
|
|
* |
|
49
|
|
|
* @author Tim Wagner <[email protected]> |
|
50
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
51
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
52
|
|
|
* @link https://github.com/techdivision/import |
|
53
|
|
|
* @link http://www.techdivision.com |
|
54
|
|
|
*/ |
|
55
|
|
|
class ArrayCache extends \ArrayObject implements CacheItemPoolInterface |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Confirms if the cache contains specified cache item. |
|
60
|
|
|
* |
|
61
|
|
|
* Note: This method MAY avoid retrieving the cached value for performance reasons. |
|
62
|
|
|
* This could result in a race condition with CacheItemInterface::get(). To avoid |
|
63
|
|
|
* such situation use CacheItemInterface::isHit() instead. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $key The key for which to check existence. |
|
66
|
|
|
* |
|
67
|
|
|
* @return boolean True if item exists in the cache, false otherwise. |
|
68
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::hasItem() |
|
69
|
|
|
*/ |
|
70
|
|
|
public function hasItem($key) |
|
71
|
|
|
{ |
|
72
|
|
|
return isset($this[$key]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Deletes all items in the pool. |
|
77
|
|
|
* |
|
78
|
|
|
* @return boolean TRUE if the pool was successfully cleared. FALSE if there was an error. |
|
79
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::clear() |
|
80
|
|
|
*/ |
|
81
|
|
|
public function clear() |
|
82
|
|
|
{ |
|
83
|
|
|
|
|
84
|
|
|
// load the iterator |
|
85
|
|
|
$iterator = $this->getIterator(); |
|
86
|
|
|
|
|
87
|
|
|
// iterate over the cached items and delete them |
|
88
|
|
|
while ($iterator->valid()) { |
|
89
|
|
|
$this->deleteItem($iterator->current()->getKey()); |
|
|
|
|
|
|
90
|
|
|
$iterator->next(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// return TRUE if we're successful |
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Persists a cache item immediately. |
|
99
|
|
|
* |
|
100
|
|
|
* @param CacheItemInterface $item The cache item to save. |
|
101
|
|
|
* |
|
102
|
|
|
* @return boolean TRUE if the item was successfully persisted. FALSE if there was an error. |
|
103
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::save() |
|
104
|
|
|
*/ |
|
105
|
|
|
public function save(CacheItemInterface $item) |
|
106
|
|
|
{ |
|
107
|
|
|
$this[$item->getKey()] = $item; |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns a Cache Item representing the specified key. |
|
113
|
|
|
* |
|
114
|
|
|
* This method must always return a CacheItemInterface object, even in case of |
|
115
|
|
|
* a cache miss. It MUST NOT return null. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $key The key for which to return the corresponding Cache Item. |
|
118
|
|
|
* |
|
119
|
|
|
* @return \Psr\Cache\CacheItemInterface The corresponding Cache Item. |
|
120
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::getItem() |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getItem($key) |
|
123
|
|
|
{ |
|
124
|
|
|
|
|
125
|
|
|
// query whether or not the item exists |
|
126
|
|
|
if ($this->hasItem($key)) { |
|
127
|
|
|
return $this[$key]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// create a new cache item and return it |
|
131
|
|
|
return new CacheItem($key); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns a traversable set of cache items. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string[] $keys An indexed array of keys of items to retrieve. |
|
138
|
|
|
* |
|
139
|
|
|
* @return array|\Traversable A traversable collection of Cache Items keyed by the cache keys of each item. A Cache item will be returned for each key, even if that key is not found. However, if no keys are specified then an empty traversable MUST be returned instead. |
|
140
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::getItems() |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getItems(array $keys = []) |
|
143
|
|
|
{ |
|
144
|
|
|
|
|
145
|
|
|
// initialize the array for the cache items |
|
146
|
|
|
$items = array(); |
|
147
|
|
|
|
|
148
|
|
|
// iterate over the passed keys and load the items |
|
149
|
|
|
foreach ($keys as $key) { |
|
150
|
|
|
$items[] = $this->getItem($key); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// return the items |
|
154
|
|
|
return $items; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Removes the item from the pool. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $key The key to delete. |
|
161
|
|
|
* |
|
162
|
|
|
* @return boolean TRUE if the item was successfully removed. FALSE if there was an error. |
|
163
|
|
|
* |
|
164
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::deleteItem() |
|
165
|
|
|
*/ |
|
166
|
|
|
public function deleteItem($key) |
|
167
|
|
|
{ |
|
168
|
|
|
unset($this[$key]); |
|
169
|
|
|
return true; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Removes multiple items from the pool. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string[] $keys An array of keys that should be removed from the pool. |
|
176
|
|
|
* |
|
177
|
|
|
* @return boolean TRUE if the items were successfully removed. False if there was an error. |
|
178
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::deleteItems() |
|
179
|
|
|
*/ |
|
180
|
|
|
public function deleteItems(array $keys) |
|
181
|
|
|
{ |
|
182
|
|
|
|
|
183
|
|
|
// iterate over the keys and delete the items |
|
184
|
|
|
foreach ($keys as $key) { |
|
185
|
|
|
$this->deleteItem($key); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// return TRUE if we've been successful |
|
189
|
|
|
return true; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Returns new ArrayIterator. |
|
194
|
|
|
* |
|
195
|
|
|
* @return \ArrayIterator|\Traversable |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getIterator() |
|
198
|
|
|
{ |
|
199
|
|
|
return new \ArrayIterator(get_object_vars($this)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Sets a cache item to be persisted later. |
|
204
|
|
|
* |
|
205
|
|
|
* @param \Psr\Cache\CacheItemInterface $item The cache item to save. |
|
206
|
|
|
* |
|
207
|
|
|
* @return boolean FALSE if the item could not be queued or if a commit was attempted and failed. True otherwise. |
|
208
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::saveDeferred() |
|
209
|
|
|
* @throws \Exception |
|
210
|
|
|
*/ |
|
211
|
|
|
public function saveDeferred(CacheItemInterface $item) |
|
212
|
|
|
{ |
|
213
|
|
|
throw new \Exception(sprintf('Method "%s" is not yet implemented', __METHOD__)); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Persists any deferred cache items. |
|
218
|
|
|
* |
|
219
|
|
|
* @return boolean TRUE if all not-yet-saved items were successfully saved or there were none. FALSE otherwise. |
|
220
|
|
|
* @see \Psr\Cache\CacheItemPoolInterface::commit() |
|
221
|
|
|
* @throws \Exception |
|
222
|
|
|
*/ |
|
223
|
|
|
public function commit() |
|
224
|
|
|
{ |
|
225
|
|
|
throw new \Exception(sprintf('Method "%s" is not yet implemented', __METHOD__)); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: