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