1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\cache; |
6
|
|
|
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
use voku\cache\Exception\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
class CachePsr16 extends Cache implements CacheInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Wipes clean the entire cache's keys. |
14
|
|
|
* |
15
|
|
|
* @return bool true on success and false on failure |
16
|
|
|
*/ |
17
|
|
|
public function clear(): bool |
18
|
|
|
{ |
19
|
|
|
return $this->removeAll(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Delete an item from the cache by its unique key. |
24
|
|
|
* |
25
|
|
|
* @param string $key the unique cache key of the item to delete |
26
|
|
|
* |
27
|
|
|
* @throws InvalidArgumentException |
28
|
|
|
* |
29
|
|
|
* @return bool True if the item was successfully removed. False if there was an error. |
30
|
|
|
*/ |
31
|
1 |
|
public function delete($key): bool |
32
|
|
|
{ |
33
|
1 |
|
if (!\is_string($key)) { |
34
|
|
|
throw new InvalidArgumentException('$key is not a string:' . \print_r($key, true)); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
return $this->removeItem($key); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Deletes multiple cache items in a single operation. |
42
|
|
|
* |
43
|
|
|
* @param iterable $keys a list of string-based keys to be deleted |
44
|
|
|
* |
45
|
|
|
* @throws InvalidArgumentException |
46
|
|
|
* |
47
|
|
|
* @return bool True if the items were successfully removed. False if there was an error. |
48
|
|
|
*/ |
49
|
|
|
public function deleteMultiple($keys): bool |
50
|
|
|
{ |
51
|
|
View Code Duplication |
if ( |
|
|
|
|
52
|
|
|
!\is_array($keys) |
53
|
|
|
&& |
54
|
|
|
!($keys instanceof \Traversable) |
55
|
|
|
) { |
56
|
|
|
throw new InvalidArgumentException('$keys is not iterable:' . \print_r($keys, true)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$results = []; |
60
|
|
|
foreach ((array) $keys as $key) { |
61
|
|
|
$results[] = $this->delete($key); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return !\in_array(false, $results, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Fetches a value from the cache. |
69
|
|
|
* |
70
|
|
|
* @param string $key the unique key of this item in the cache |
71
|
|
|
* @param mixed $default default value to return if the key does not exist |
72
|
|
|
* |
73
|
|
|
* @throws InvalidArgumentException |
74
|
|
|
* |
75
|
|
|
* @return mixed the value of the item from the cache, or $default in case of cache miss |
76
|
|
|
*/ |
77
|
3 |
|
public function get($key, $default = null) |
78
|
|
|
{ |
79
|
3 |
|
if ($this->has($key)) { |
80
|
1 |
|
return $this->getItem($key); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
return $default; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Obtains multiple cache items by their unique keys. |
88
|
|
|
* |
89
|
|
|
* @param iterable $keys a list of keys that can obtained in a single operation |
90
|
|
|
* @param mixed $default default value to return for keys that do not exist |
91
|
|
|
* |
92
|
|
|
* @throws InvalidArgumentException |
93
|
|
|
* |
94
|
|
|
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as |
95
|
|
|
* value. |
96
|
|
|
*/ |
97
|
|
|
public function getMultiple($keys, $default = null) |
98
|
|
|
{ |
99
|
|
View Code Duplication |
if ( |
|
|
|
|
100
|
|
|
!\is_array($keys) |
101
|
|
|
&& |
102
|
|
|
!($keys instanceof \Traversable) |
103
|
|
|
) { |
104
|
|
|
throw new InvalidArgumentException('$keys is not iterable:' . \print_r($keys, true)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$result = []; |
108
|
|
|
foreach ((array) $keys as $key) { |
109
|
|
|
$result[$key] = $this->has($key) ? $this->get($key) : $default; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $result; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Determines whether an item is present in the cache. |
117
|
|
|
* |
118
|
|
|
* NOTE: It is recommended that has() is only to be used for cache warming type purposes |
119
|
|
|
* and not to be used within your live applications operations for get/set, as this method |
120
|
|
|
* is subject to a race condition where your has() will return true and immediately after, |
121
|
|
|
* another script can remove it making the state of your app out of date. |
122
|
|
|
* |
123
|
|
|
* @param string $key the cache item key |
124
|
|
|
* |
125
|
|
|
* @throws InvalidArgumentException |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
4 |
|
public function has($key): bool |
130
|
|
|
{ |
131
|
4 |
|
if (!\is_string($key)) { |
132
|
|
|
throw new InvalidArgumentException('$key is not a string:' . \print_r($key, true)); |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
return $this->existsItem($key); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
140
|
|
|
* |
141
|
|
|
* @param string $key the key of the item to store |
142
|
|
|
* @param mixed $value the value of the item to store, must be serializable |
143
|
|
|
* @param \DateInterval|int|null $ttl Optional. The TTL value of this item. If no value is sent and |
144
|
|
|
* the driver supports TTL then the library may set a default value |
145
|
|
|
* for it or let the driver take care of that. |
146
|
|
|
* |
147
|
|
|
* @throws InvalidArgumentException |
148
|
|
|
* |
149
|
|
|
* @return bool true on success and false on failure |
150
|
|
|
*/ |
151
|
3 |
|
public function set($key, $value, $ttl = null): bool |
152
|
|
|
{ |
153
|
3 |
|
if (!\is_string($key)) { |
154
|
|
|
throw new InvalidArgumentException('$key is not a string:' . \print_r($key, true)); |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
return $this->setItem($key, $value, $ttl); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Persists a set of key => value pairs in the cache, with an optional TTL. |
162
|
|
|
* |
163
|
|
|
* @param iterable $values a list of key => value pairs for a multiple-set operation |
164
|
|
|
* @param \DateInterval|int|null $ttl Optional. The TTL value of this item. If no value is sent and |
165
|
|
|
* the driver supports TTL then the library may set a default value |
166
|
|
|
* for it or let the driver take care of that. |
167
|
|
|
* |
168
|
|
|
* @throws InvalidArgumentException |
169
|
|
|
* |
170
|
|
|
* @return bool true on success and false on failure |
171
|
|
|
*/ |
172
|
|
|
public function setMultiple($values, $ttl = null): bool |
173
|
|
|
{ |
174
|
|
View Code Duplication |
if ( |
|
|
|
|
175
|
|
|
!\is_array($values) |
176
|
|
|
&& |
177
|
|
|
!($values instanceof \Traversable) |
178
|
|
|
) { |
179
|
|
|
throw new InvalidArgumentException('$values is not iterable:' . \print_r($values, true)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$results = []; |
183
|
|
|
foreach ((array) $values as $key => $value) { |
184
|
|
|
$results[] = $this->set($key, $value, $ttl); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return !\in_array(false, $results, true); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
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.