/
SimpleCacheEngine.php
220 lines (203 loc) · 7.45 KB
/
SimpleCacheEngine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.7.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Cache;
use Psr\SimpleCache\CacheInterface;
/**
* Wrapper for Cake engines that allow them to support
* the PSR16 Simple Cache Interface
*
* @since 3.7.0
* @link https://www.php-fig.org/psr/psr-16/
*/
class SimpleCacheEngine implements CacheInterface
{
/**
* The wrapped cache engine object.
*
* @param \Cake\Cache\CacheEngine
*/
protected $innerEngine;
/**
* Constructor
*
* @param \Cake\Cache\CacheEngine $innerEngine The decorated engine.
*/
public function __construct($innerEngine)
{
$this->innerEngine = $innerEngine;
}
/**
* Ensure the validity of the cache key.
*
* @param string $key Key to check.
* @return void
* @throws \Psr\SimpleCache\InvalidArgumentException When the key is not valid.
*/
protected function ensureValidKey($key)
{
if (!is_string($key) || strlen($key) === 0) {
throw new InvalidArgumentException('A cache key must be a non-empty string.');
}
}
/**
* Fetches the value for a given key from the cache.
*
* @param string $key The unique key of this item in the cache.
* @param mixed $default Default value to return if the key does not exist.
* @return mixed The value of the item from the cache, or $default in case of cache miss.
* @throws \Psr\SimpleCache\InvalidArgumentException If the $key string is not a legal value.
*/
public function get($key, $default = null)
{
$this->ensureValidKey($key);
$result = $this->innerEngine->read($key);
if ($result === false) {
return $default;
}
return $result;
}
/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
* @return bool True on success and false on failure.
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function set($key, $value, $ttl = null)
{
$this->ensureValidKey($key);
if ($ttl !== null) {
$restore = $this->innerEngine->getConfig('duration');
$this->innerEngine->setConfig('duration', $ttl);
}
try {
$result = $this->innerEngine->write($key, $value);
return (bool)$result;
} finally {
if (isset($restore)) {
$this->innerEngine->setConfig('duration', $restore);
}
}
}
/**
* Delete an item from the cache by its unique key.
*
* @param string $key The unique cache key of the item to delete.
* @return bool True if the item was successfully removed. False if there was an error.
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function delete($key)
{
$this->ensureValidKey($key);
return $this->innerEngine->delete($key);
}
/**
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
*/
public function clear()
{
return $this->innerEngine->clear(false);
}
/**
* Obtains multiple cache items by their unique keys.
*
* @param iterable $keys A list of keys that can obtained in a single operation.
* @param mixed $default Default value to return for keys that do not exist.
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if $keys is neither an array nor a Traversable,
* or if any of the $keys are not a legal value.
*/
public function getMultiple($keys, $default = null)
{
$results = $this->innerEngine->readMany($keys);
foreach ($results as $key => $value) {
if ($value === false) {
$results[$key] = $default;
}
}
return $results;
}
/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*
* @param iterable $values A list of key => value pairs for a multiple-set operation.
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
* @return bool True on success and false on failure.
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if $values is neither an array nor a Traversable,
* or if any of the $values are not a legal value.
*/
public function setMultiple($values, $ttl = null)
{
if ($ttl !== null) {
$restore = $this->innerEngine->getConfig('duration');
$this->innerEngine->setConfig('duration', $ttl);
}
try {
return $this->innerEngine->writeMany($values);
} finally {
if (isset($restore)) {
$this->innerEngine->setConfig('duration', $restore);
}
}
}
/**
* Deletes multiple cache items in a single operation.
*
* @param iterable $keys A list of string-based keys to be deleted.
* @return bool True if the items were successfully removed. False if there was an error.
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if $keys is neither an array nor a Traversable,
* or if any of the $keys are not a legal value.
*/
public function deleteMultiple($keys)
{
$result = $this->innerEngine->deleteMany($keys);
foreach ($result as $key => $success) {
if ($success === false) {
return false;
}
}
return true;
}
/**
* Determines whether an item is present in the cache.
*
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
* and not to be used within your live applications operations for get/set, as this method
* is subject to a race condition where your has() will return true and immediately after,
* another script can remove it making the state of your app out of date.
*
* @param string $key The cache item key.
* @return bool
* @throws \Psr\SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function has($key)
{
return $this->get($key) !== null;
}
}