CacheEngine.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Cache;
  17. use Cake\Cache\Exception\InvalidArgumentException;
  18. use Cake\Core\InstanceConfigTrait;
  19. use DateInterval;
  20. use DateTime;
  21. use Psr\SimpleCache\CacheInterface;
  22. use function Cake\Core\triggerWarning;
  23. /**
  24. * Storage engine for CakePHP caching
  25. */
  26. abstract class CacheEngine implements CacheInterface, CacheEngineInterface
  27. {
  28. use InstanceConfigTrait;
  29. /**
  30. * @var string
  31. */
  32. protected const CHECK_KEY = 'key';
  33. /**
  34. * @var string
  35. */
  36. protected const CHECK_VALUE = 'value';
  37. /**
  38. * The default cache configuration is overridden in most cache adapters. These are
  39. * the keys that are common to all adapters. If overridden, this property is not used.
  40. *
  41. * - `duration` Specify how long items in this cache configuration last.
  42. * - `groups` List of groups or 'tags' associated to every key stored in this config.
  43. * handy for deleting a complete group from cache.
  44. * - `prefix` Prefix appended to all entries. Good for when you need to share a keyspace
  45. * with either another cache config or another application.
  46. * - `warnOnWriteFailures` Some engines, such as ApcuEngine, may raise warnings on
  47. * write failures.
  48. *
  49. * @var array<string, mixed>
  50. */
  51. protected array $_defaultConfig = [
  52. 'duration' => 3600,
  53. 'groups' => [],
  54. 'prefix' => 'cake_',
  55. 'warnOnWriteFailures' => true,
  56. ];
  57. /**
  58. * Contains the compiled string with all group
  59. * prefixes to be prepended to every key in this cache engine
  60. *
  61. * @var string
  62. */
  63. protected string $_groupPrefix = '';
  64. /**
  65. * Initialize the cache engine
  66. *
  67. * Called automatically by the cache frontend. Merge the runtime config with the defaults
  68. * before use.
  69. *
  70. * @param array<string, mixed> $config Associative array of parameters for the engine
  71. * @return bool True if the engine has been successfully initialized, false if not
  72. */
  73. public function init(array $config = []): bool
  74. {
  75. $this->setConfig($config);
  76. if (!empty($this->_config['groups'])) {
  77. sort($this->_config['groups']);
  78. $this->_groupPrefix = str_repeat('%s_', count($this->_config['groups']));
  79. }
  80. if (!is_numeric($this->_config['duration'])) {
  81. $this->_config['duration'] = strtotime($this->_config['duration']) - time();
  82. }
  83. return true;
  84. }
  85. /**
  86. * Ensure the validity of the given cache key.
  87. *
  88. * @param mixed $key Key to check.
  89. * @return void
  90. * @throws \Cake\Cache\Exception\InvalidArgumentException When the key is not valid.
  91. */
  92. protected function ensureValidKey(mixed $key): void
  93. {
  94. if (!is_string($key) || strlen($key) === 0) {
  95. throw new InvalidArgumentException('A cache key must be a non-empty string.');
  96. }
  97. }
  98. /**
  99. * Ensure the validity of the argument type and cache keys.
  100. *
  101. * @param iterable $iterable The iterable to check.
  102. * @param string $check Whether to check keys or values.
  103. * @return void
  104. * @throws \Cake\Cache\Exception\InvalidArgumentException
  105. */
  106. protected function ensureValidType(iterable $iterable, string $check = self::CHECK_VALUE): void
  107. {
  108. foreach ($iterable as $key => $value) {
  109. if ($check === self::CHECK_VALUE) {
  110. $this->ensureValidKey($value);
  111. } else {
  112. $this->ensureValidKey($key);
  113. }
  114. }
  115. }
  116. /**
  117. * Obtains multiple cache items by their unique keys.
  118. *
  119. * @param iterable<string> $keys A list of keys that can obtained in a single operation.
  120. * @param mixed $default Default value to return for keys that do not exist.
  121. * @return iterable<string, mixed> A list of key value pairs. Cache keys that do not exist or are stale will have $default as value.
  122. * @throws \Cake\Cache\Exception\InvalidArgumentException If $keys is neither an array nor a Traversable,
  123. * or if any of the $keys are not a legal value.
  124. */
  125. public function getMultiple(iterable $keys, mixed $default = null): iterable
  126. {
  127. $this->ensureValidType($keys);
  128. $results = [];
  129. foreach ($keys as $key) {
  130. $results[$key] = $this->get($key, $default);
  131. }
  132. return $results;
  133. }
  134. /**
  135. * Persists a set of key => value pairs in the cache, with an optional TTL.
  136. *
  137. * @param iterable $values A list of key => value pairs for a multiple-set operation.
  138. * @param \DateInterval|int|null $ttl Optional. The TTL value of this item. If no value is sent and
  139. * the driver supports TTL then the library may set a default value
  140. * for it or let the driver take care of that.
  141. * @return bool True on success and false on failure.
  142. * @throws \Cake\Cache\Exception\InvalidArgumentException If $values is neither an array nor a Traversable,
  143. * or if any of the $values are not a legal value.
  144. */
  145. public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool
  146. {
  147. $this->ensureValidType($values, self::CHECK_KEY);
  148. $restore = null;
  149. if ($ttl !== null) {
  150. $restore = $this->getConfig('duration');
  151. $this->setConfig('duration', $ttl);
  152. }
  153. try {
  154. foreach ($values as $key => $value) {
  155. $success = $this->set($key, $value);
  156. if ($success === false) {
  157. return false;
  158. }
  159. }
  160. return true;
  161. } finally {
  162. if ($restore !== null) {
  163. $this->setConfig('duration', $restore);
  164. }
  165. }
  166. }
  167. /**
  168. * Deletes multiple cache items as a list
  169. *
  170. * This is a best effort attempt. If deleting an item would
  171. * create an error it will be ignored, and all items will
  172. * be attempted.
  173. *
  174. * @param iterable $keys A list of string-based keys to be deleted.
  175. * @return bool True if the items were successfully removed. False if there was an error.
  176. * @throws \Cake\Cache\Exception\InvalidArgumentException If $keys is neither an array nor a Traversable,
  177. * or if any of the $keys are not a legal value.
  178. */
  179. public function deleteMultiple(iterable $keys): bool
  180. {
  181. $this->ensureValidType($keys);
  182. $result = true;
  183. foreach ($keys as $key) {
  184. if (!$this->delete($key)) {
  185. $result = false;
  186. }
  187. }
  188. return $result;
  189. }
  190. /**
  191. * Determines whether an item is present in the cache.
  192. *
  193. * NOTE: It is recommended that has() is only to be used for cache warming type purposes
  194. * and not to be used within your live applications operations for get/set, as this method
  195. * is subject to a race condition where your has() will return true and immediately after,
  196. * another script can remove it making the state of your app out of date.
  197. *
  198. * @param string $key The cache item key.
  199. * @return bool
  200. * @throws \Cake\Cache\Exception\InvalidArgumentException If the $key string is not a legal value.
  201. */
  202. public function has(string $key): bool
  203. {
  204. return $this->get($key) !== null;
  205. }
  206. /**
  207. * Fetches the value for a given key from the cache.
  208. *
  209. * @param string $key The unique key of this item in the cache.
  210. * @param mixed $default Default value to return if the key does not exist.
  211. * @return mixed The value of the item from the cache, or $default in case of cache miss.
  212. * @throws \Cake\Cache\Exception\InvalidArgumentException If the $key string is not a legal value.
  213. */
  214. abstract public function get(string $key, mixed $default = null): mixed;
  215. /**
  216. * Persists data in the cache, uniquely referenced by the given key with an optional expiration TTL time.
  217. *
  218. * @param string $key The key of the item to store.
  219. * @param mixed $value The value of the item to store, must be serializable.
  220. * @param \DateInterval|int|null $ttl Optional. The TTL value of this item. If no value is sent and
  221. * the driver supports TTL then the library may set a default value
  222. * for it or let the driver take care of that.
  223. * @return bool True on success and false on failure.
  224. * @throws \Cake\Cache\Exception\InvalidArgumentException
  225. * MUST be thrown if the $key string is not a legal value.
  226. */
  227. abstract public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool;
  228. /**
  229. * Increment a number under the key and return incremented value
  230. *
  231. * @param string $key Identifier for the data
  232. * @param int $offset How much to add
  233. * @return int|false New incremented value, false otherwise
  234. */
  235. abstract public function increment(string $key, int $offset = 1): int|false;
  236. /**
  237. * Decrement a number under the key and return decremented value
  238. *
  239. * @param string $key Identifier for the data
  240. * @param int $offset How much to subtract
  241. * @return int|false New incremented value, false otherwise
  242. */
  243. abstract public function decrement(string $key, int $offset = 1): int|false;
  244. /**
  245. * Delete a key from the cache
  246. *
  247. * @param string $key Identifier for the data
  248. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  249. */
  250. abstract public function delete(string $key): bool;
  251. /**
  252. * Delete all keys from the cache
  253. *
  254. * @return bool True if the cache was successfully cleared, false otherwise
  255. */
  256. abstract public function clear(): bool;
  257. /**
  258. * Add a key to the cache if it does not already exist.
  259. *
  260. * Defaults to a non-atomic implementation. Subclasses should
  261. * prefer atomic implementations.
  262. *
  263. * @param string $key Identifier for the data.
  264. * @param mixed $value Data to be cached.
  265. * @return bool True if the data was successfully cached, false on failure.
  266. */
  267. public function add(string $key, mixed $value): bool
  268. {
  269. $cachedValue = $this->get($key);
  270. if ($cachedValue === null) {
  271. return $this->set($key, $value);
  272. }
  273. return false;
  274. }
  275. /**
  276. * Clears all values belonging to a group. Is up to the implementing engine
  277. * to decide whether actually delete the keys or just simulate it to achieve
  278. * the same result.
  279. *
  280. * @param string $group name of the group to be cleared
  281. * @return bool
  282. */
  283. abstract public function clearGroup(string $group): bool;
  284. /**
  285. * Does whatever initialization for each group is required
  286. * and returns the `group value` for each of them, this is
  287. * the token representing each group in the cache key
  288. *
  289. * @return array<string>
  290. */
  291. public function groups(): array
  292. {
  293. return $this->_config['groups'];
  294. }
  295. /**
  296. * Generates a key for cache backend usage.
  297. *
  298. * If the requested key is valid, the group prefix value and engine prefix are applied.
  299. * Whitespace in keys will be replaced.
  300. *
  301. * @param string $key the key passed over
  302. * @return string Prefixed key with potentially unsafe characters replaced.
  303. * @throws \Cake\Cache\Exception\InvalidArgumentException If key's value is invalid.
  304. */
  305. protected function _key(string $key): string
  306. {
  307. $this->ensureValidKey($key);
  308. $prefix = '';
  309. if ($this->_groupPrefix) {
  310. $prefix = md5(implode('_', $this->groups()));
  311. }
  312. $key = preg_replace('/[\s]+/', '_', $key);
  313. return $this->_config['prefix'] . $prefix . $key;
  314. }
  315. /**
  316. * Cache Engines may trigger warnings if they encounter failures during operation,
  317. * if option warnOnWriteFailures is set to true.
  318. *
  319. * @param string $message The warning message.
  320. * @return void
  321. */
  322. protected function warning(string $message): void
  323. {
  324. if ($this->getConfig('warnOnWriteFailures') !== true) {
  325. return;
  326. }
  327. triggerWarning($message);
  328. }
  329. /**
  330. * Convert the various expressions of a TTL value into duration in seconds
  331. *
  332. * @param \DateInterval|int|null $ttl The TTL value of this item. If null is sent, the
  333. * driver's default duration will be used.
  334. * @return int
  335. */
  336. protected function duration(DateInterval|int|null $ttl): int
  337. {
  338. if ($ttl === null) {
  339. return $this->_config['duration'];
  340. }
  341. if (is_int($ttl)) {
  342. return $ttl;
  343. }
  344. /** @var \DateTime $datetime */
  345. $datetime = DateTime::createFromFormat('U', '0');
  346. return (int)$datetime
  347. ->add($ttl)
  348. ->format('U');
  349. }
  350. }