1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2014-2018 |
6
|
|
|
* @package MW |
7
|
|
|
* @subpackage Cache |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\MW\Cache; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Flow caching implementation. |
16
|
|
|
* |
17
|
|
|
* @package MW |
18
|
|
|
* @subpackage Cache |
19
|
|
|
*/ |
20
|
|
|
class Flow |
21
|
|
|
extends \Aimeos\MW\Cache\Base |
22
|
|
|
implements \Aimeos\MW\Cache\Iface |
23
|
|
|
{ |
24
|
|
|
private $object; |
25
|
|
|
private $prefix; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initializes the object instance. |
30
|
|
|
* |
31
|
|
|
* @param array $config List of configuration values |
32
|
|
|
* @param \Neos\Cache\Frontend\FrontendInterface $cache Neos cache object |
33
|
|
|
*/ |
34
|
|
|
public function __construct( array $config, \Neos\Cache\Frontend\FrontendInterface $cache ) |
35
|
|
|
{ |
36
|
|
|
$this->prefix = ( isset( $config['siteid'] ) ? $config['siteid'] . '-' : '' ); |
37
|
|
|
$this->object = $cache; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Removes all expired cache entries. |
43
|
|
|
* |
44
|
|
|
* @inheritDoc |
45
|
|
|
* |
46
|
|
|
* @return bool True on success and false on failure |
47
|
|
|
*/ |
48
|
|
|
public function cleanup() : bool |
49
|
|
|
{ |
50
|
|
|
$this->object->collectGarbage(); |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Removes all entries for the current site from the cache. |
57
|
|
|
* |
58
|
|
|
* @inheritDoc |
59
|
|
|
* |
60
|
|
|
* @return bool True on success and false on failure |
61
|
|
|
*/ |
62
|
|
|
public function clear() : bool |
63
|
|
|
{ |
64
|
|
|
if( $this->prefix ) { |
65
|
|
|
$this->object->flushByTag( $this->prefix . 'siteid' ); |
66
|
|
|
} else { |
67
|
|
|
$this->object->flush(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Removes the cache entry identified by the given key. |
76
|
|
|
* |
77
|
|
|
* @inheritDoc |
78
|
|
|
* |
79
|
|
|
* @param string $key Key string that identifies the single cache entry |
80
|
|
|
* @return bool True if the item was successfully removed. False if there was an error |
81
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
82
|
|
|
*/ |
83
|
|
|
public function delete( string $key ) : bool |
84
|
|
|
{ |
85
|
|
|
$this->object->remove( $this->prefix . $key ); |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Removes the cache entries identified by the given keys. |
92
|
|
|
* |
93
|
|
|
* @inheritDoc |
94
|
|
|
* |
95
|
|
|
* @param iterable $keys List of key strings that identify the cache entries that should be removed |
96
|
|
|
* @return bool True if the items were successfully removed. False if there was an error. |
97
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public function deleteMultiple( iterable $keys ) : bool |
100
|
|
|
{ |
101
|
|
|
foreach( $keys as $key ) { |
102
|
|
|
$this->object->remove( $this->prefix . $key ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Removes the cache entries identified by the given tags. |
111
|
|
|
* |
112
|
|
|
* @inheritDoc |
113
|
|
|
* |
114
|
|
|
* @param iterable $tags List of tag strings that are associated to one or |
115
|
|
|
* more cache entries that should be removed |
116
|
|
|
* @return bool True if the items were successfully removed. False if there was an error. |
117
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
118
|
|
|
*/ |
119
|
|
|
public function deleteByTags( iterable $tags ) : bool |
120
|
|
|
{ |
121
|
|
|
foreach( $tags as $tag ) { |
122
|
|
|
$this->object->flushByTag( $this->prefix . $tag ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the value of the requested cache key. |
131
|
|
|
* |
132
|
|
|
* @inheritDoc |
133
|
|
|
* |
134
|
|
|
* @param string $key Path to the requested value like product/id/123 |
135
|
|
|
* @param mixed $default Value returned if requested key isn't found |
136
|
|
|
* @return mixed Value associated to the requested key. If no value for the |
137
|
|
|
* key is found in the cache, the given default value is returned |
138
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
139
|
|
|
*/ |
140
|
|
|
public function get( string $key, $default = null ) |
141
|
|
|
{ |
142
|
|
|
if( ( $entry = $this->object->get( $this->prefix . $key ) ) !== false ) { |
143
|
|
|
return $entry; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $default; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns the cached values for the given cache keys. |
152
|
|
|
* |
153
|
|
|
* @inheritDoc |
154
|
|
|
* |
155
|
|
|
* @param iterable $keys List of key strings for the requested cache entries |
156
|
|
|
* @param mixed $default Default value to return for keys that do not exist |
157
|
|
|
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
158
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
159
|
|
|
*/ |
160
|
|
|
public function getMultiple( iterable $keys, $default = null ) : iterable |
161
|
|
|
{ |
162
|
|
|
$result = []; |
163
|
|
|
|
164
|
|
|
foreach( $keys as $key ) |
165
|
|
|
{ |
166
|
|
|
if( ( $entry = $this->object->get( $this->prefix . $key ) ) !== false ) { |
167
|
|
|
$result[$key] = $entry; |
168
|
|
|
} else { |
169
|
|
|
$result[$key] = $default; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $result; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Determines whether an item is present in the cache. |
179
|
|
|
* |
180
|
|
|
* @inheritDoc |
181
|
|
|
* |
182
|
|
|
* @param string $key The cache item key |
183
|
|
|
* @return bool True if cache entry is available, false if not |
184
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
185
|
|
|
*/ |
186
|
|
|
public function has( string $key ) : bool |
187
|
|
|
{ |
188
|
|
|
return $this->object->has( $this->prefix . $key ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Sets the value for the given key in the cache. |
194
|
|
|
* |
195
|
|
|
* @inheritDoc |
196
|
|
|
* |
197
|
|
|
* @param string $key Key string for the given value like product/id/123 |
198
|
|
|
* @param mixed $value Value string that should be stored for the given key |
199
|
|
|
* @param \DateInterval|int|string|null $expires Date interval object, |
200
|
|
|
* date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value |
201
|
|
|
* when the cache entry will expiry |
202
|
|
|
* @param iterable $tags List of tag strings that should be assoicated to the cache entry |
203
|
|
|
* @return bool True on success and false on failure. |
204
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
205
|
|
|
*/ |
206
|
|
|
public function set( string $key, $value, $expires = null, iterable $tags = [] ) : bool |
207
|
|
|
{ |
208
|
|
|
if( $expires instanceof \DateInterval ) { |
209
|
|
|
$expires = date_create()->add( $expires )->getTimestamp() - time(); |
210
|
|
|
} elseif( is_string( $expires ) ) { |
211
|
|
|
$expires = date_create( $expires )->getTimestamp() - time(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$tagList = ( $this->prefix ? array( $this->prefix . 'siteid' ) : [] ); |
215
|
|
|
|
216
|
|
|
foreach( $tags as $tag ) { |
217
|
|
|
$tagList[] = $this->prefix . $tag; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->object->set( $this->prefix . $key, $value, $tagList, $expires ); |
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Adds or overwrites the given key/value pairs in the cache, which is much |
227
|
|
|
* more efficient than setting them one by one using the set() method. |
228
|
|
|
* |
229
|
|
|
* @inheritDoc |
230
|
|
|
* |
231
|
|
|
* @param iterable $pairs Associative list of key/value pairs. Both must be a string |
232
|
|
|
* @param \DateInterval|int|string|null $expires Date interval object, |
233
|
|
|
* date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value |
234
|
|
|
* when the cache entry will expiry |
235
|
|
|
* @param iterable $tags List of tags that should be associated to the cache entries |
236
|
|
|
* @return bool True on success and false on failure. |
237
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
238
|
|
|
*/ |
239
|
|
|
public function setMultiple( iterable $pairs, $expires = null, iterable $tags = [] ) : bool |
240
|
|
|
{ |
241
|
|
|
foreach( $pairs as $key => $value ) { |
242
|
|
|
$this->set( $key, $value, $expires, $tags ); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return true; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|