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