From adbfa052d6f203319478e311fb57e1a14a058bb5 Mon Sep 17 00:00:00 2001 From: Corey Taylor Date: Thu, 17 Jun 2021 22:44:36 -0500 Subject: [PATCH 01/36] Normalize order of union types in docblocks --- CacheRegistry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CacheRegistry.php b/CacheRegistry.php index fb6d0ab06..464689737 100644 --- a/CacheRegistry.php +++ b/CacheRegistry.php @@ -64,7 +64,7 @@ protected function _throwMissingClassError(string $class, ?string $plugin): void * * Part of the template method for Cake\Core\ObjectRegistry::load() * - * @param string|\Cake\Cache\CacheEngine $class The classname or object to make. + * @param \Cake\Cache\CacheEngine|string $class The classname or object to make. * @param string $alias The alias of the object. * @param array $config An array of settings to use for the cache engine. * @return \Cake\Cache\CacheEngine The constructed CacheEngine class. From 57dca48ae59fddc2671bfcd2b12a60a219b2b0d8 Mon Sep 17 00:00:00 2001 From: Corey Taylor Date: Wed, 14 Jul 2021 02:22:04 -0500 Subject: [PATCH 02/36] Convert single type[] array hints to array hints --- Cache.php | 4 ++-- CacheEngine.php | 2 +- Engine/ApcuEngine.php | 4 ++-- Engine/ArrayEngine.php | 2 +- Engine/MemcachedEngine.php | 4 ++-- Engine/RedisEngine.php | 2 +- Engine/WincacheEngine.php | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cache.php b/Cache.php index fabf32a3a..ddc6269b6 100644 --- a/Cache.php +++ b/Cache.php @@ -71,7 +71,7 @@ class Cache * An array mapping URL schemes to fully qualified caching engine * class names. * - * @var string[] + * @var array * @psalm-var array */ protected static $_dsnClassMap = [ @@ -466,7 +466,7 @@ public static function clear(string $config = 'default'): bool /** * Delete all keys from the cache from all configurations. * - * @return bool[] Status code. For each configuration, it reports the status of the operation + * @return array Status code. For each configuration, it reports the status of the operation */ public static function clearAll(): array { diff --git a/CacheEngine.php b/CacheEngine.php index 56db69536..9c26353ee 100644 --- a/CacheEngine.php +++ b/CacheEngine.php @@ -317,7 +317,7 @@ abstract public function clearGroup(string $group): bool; * and returns the `group value` for each of them, this is * the token representing each group in the cache key * - * @return string[] + * @return array */ public function groups(): array { diff --git a/Engine/ApcuEngine.php b/Engine/ApcuEngine.php index 170ea4a43..085277953 100644 --- a/Engine/ApcuEngine.php +++ b/Engine/ApcuEngine.php @@ -29,7 +29,7 @@ class ApcuEngine extends CacheEngine * Contains the compiled group names * (prefixed with the global configuration prefix) * - * @var string[] + * @var array */ protected $_compiledGroupNames = []; @@ -183,7 +183,7 @@ public function add(string $key, $value): bool * If the group initial value was not found, then it initializes * the group accordingly. * - * @return string[] + * @return array * @link https://secure.php.net/manual/en/function.apcu-fetch.php * @link https://secure.php.net/manual/en/function.apcu-store.php */ diff --git a/Engine/ArrayEngine.php b/Engine/ArrayEngine.php index c33608204..9050537eb 100644 --- a/Engine/ArrayEngine.php +++ b/Engine/ArrayEngine.php @@ -152,7 +152,7 @@ public function clear(): bool * If the group initial value was not found, then it initializes * the group accordingly. * - * @return string[] + * @return array */ public function groups(): array { diff --git a/Engine/MemcachedEngine.php b/Engine/MemcachedEngine.php index 781c4cfc7..e0f273834 100644 --- a/Engine/MemcachedEngine.php +++ b/Engine/MemcachedEngine.php @@ -87,7 +87,7 @@ class MemcachedEngine extends CacheEngine protected $_serializers = []; /** - * @var string[] + * @var array */ protected $_compiledGroupNames = []; @@ -470,7 +470,7 @@ public function add(string $key, $value): bool * If the group initial value was not found, then it initializes * the group accordingly. * - * @return string[] + * @return array */ public function groups(): array { diff --git a/Engine/RedisEngine.php b/Engine/RedisEngine.php index 1eeed8cad..0a94bd94f 100644 --- a/Engine/RedisEngine.php +++ b/Engine/RedisEngine.php @@ -283,7 +283,7 @@ public function add(string $key, $value): bool * If the group initial value was not found, then it initializes * the group accordingly. * - * @return string[] + * @return array */ public function groups(): array { diff --git a/Engine/WincacheEngine.php b/Engine/WincacheEngine.php index 9d9e36eb6..4936f1188 100644 --- a/Engine/WincacheEngine.php +++ b/Engine/WincacheEngine.php @@ -155,7 +155,7 @@ public function clear(): bool * If the group initial value was not found, then it initializes * the group accordingly. * - * @return string[] + * @return array */ public function groups(): array { From 64df16c78a486f586ec14ca16192d99bbdf1b10c Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 31 Aug 2021 22:16:09 +0200 Subject: [PATCH 03/36] Start documenting the assoc arrays as per their string key. --- Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cache.php b/Cache.php index ddc6269b6..01675a0b3 100644 --- a/Cache.php +++ b/Cache.php @@ -466,7 +466,7 @@ public static function clear(string $config = 'default'): bool /** * Delete all keys from the cache from all configurations. * - * @return array Status code. For each configuration, it reports the status of the operation + * @return array Status code. For each configuration, it reports the status of the operation */ public static function clearAll(): array { From 3b76a9b728f5bf1106bcec3c45f1e5e51c306d17 Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 3 Sep 2021 16:00:27 +0200 Subject: [PATCH 04/36] Document the assoc arrays as per their string key. --- CacheEngine.php | 4 ++-- CacheRegistry.php | 2 +- Engine/ApcuEngine.php | 2 +- Engine/FileEngine.php | 4 ++-- Engine/MemcachedEngine.php | 4 ++-- Engine/RedisEngine.php | 4 ++-- Engine/WincacheEngine.php | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CacheEngine.php b/CacheEngine.php index 9c26353ee..8e9fc613a 100644 --- a/CacheEngine.php +++ b/CacheEngine.php @@ -49,7 +49,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface * - `warnOnWriteFailures` Some engines, such as ApcuEngine, may raise warnings on * write failures. * - * @var array + * @var array */ protected $_defaultConfig = [ 'duration' => 3600, @@ -72,7 +72,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface * Called automatically by the cache frontend. Merge the runtime config with the defaults * before use. * - * @param array $config Associative array of parameters for the engine + * @param array $config Associative array of parameters for the engine * @return bool True if the engine has been successfully initialized, false if not */ public function init(array $config = []): bool diff --git a/CacheRegistry.php b/CacheRegistry.php index 464689737..c70835fc0 100644 --- a/CacheRegistry.php +++ b/CacheRegistry.php @@ -66,7 +66,7 @@ protected function _throwMissingClassError(string $class, ?string $plugin): void * * @param \Cake\Cache\CacheEngine|string $class The classname or object to make. * @param string $alias The alias of the object. - * @param array $config An array of settings to use for the cache engine. + * @param array $config An array of settings to use for the cache engine. * @return \Cake\Cache\CacheEngine The constructed CacheEngine class. * @throws \RuntimeException when an object doesn't implement the correct interface. */ diff --git a/Engine/ApcuEngine.php b/Engine/ApcuEngine.php index 085277953..bc90e8f0f 100644 --- a/Engine/ApcuEngine.php +++ b/Engine/ApcuEngine.php @@ -38,7 +38,7 @@ class ApcuEngine extends CacheEngine * * Called automatically by the cache frontend * - * @param array $config array of setting for the engine + * @param array $config array of setting for the engine * @return bool True if the engine has been successfully initialized, false if not */ public function init(array $config = []): bool diff --git a/Engine/FileEngine.php b/Engine/FileEngine.php index 22ec33ce0..e7ce5ea2c 100644 --- a/Engine/FileEngine.php +++ b/Engine/FileEngine.php @@ -57,7 +57,7 @@ class FileEngine extends CacheEngine * cache::gc from ever being called automatically. * - `serialize` Should cache objects be serialized first. * - * @var array + * @var array */ protected $_defaultConfig = [ 'duration' => 3600, @@ -81,7 +81,7 @@ class FileEngine extends CacheEngine * * Called automatically by the cache frontend. * - * @param array $config array of setting for the engine + * @param array $config array of setting for the engine * @return bool True if the engine has been successfully initialized, false if not */ public function init(array $config = []): bool diff --git a/Engine/MemcachedEngine.php b/Engine/MemcachedEngine.php index e0f273834..227161513 100644 --- a/Engine/MemcachedEngine.php +++ b/Engine/MemcachedEngine.php @@ -60,7 +60,7 @@ class MemcachedEngine extends CacheEngine * - `options` - Additional options for the memcached client. Should be an array of option => value. * Use the \Memcached::OPT_* constants as keys. * - * @var array + * @var array */ protected $_defaultConfig = [ 'compress' => false, @@ -96,7 +96,7 @@ class MemcachedEngine extends CacheEngine * * Called automatically by the cache frontend * - * @param array $config array of setting for the engine + * @param array $config array of setting for the engine * @return bool True if the engine has been successfully initialized, false if not * @throws \InvalidArgumentException When you try use authentication without * Memcached compiled with SASL support diff --git a/Engine/RedisEngine.php b/Engine/RedisEngine.php index 0a94bd94f..b7f0fec4b 100644 --- a/Engine/RedisEngine.php +++ b/Engine/RedisEngine.php @@ -51,7 +51,7 @@ class RedisEngine extends CacheEngine * - `timeout` timeout in seconds (float). * - `unix_socket` Path to the unix socket file (default: false) * - * @var array + * @var array */ protected $_defaultConfig = [ 'database' => 0, @@ -72,7 +72,7 @@ class RedisEngine extends CacheEngine * * Called automatically by the cache frontend * - * @param array $config array of setting for the engine + * @param array $config array of setting for the engine * @return bool True if the engine has been successfully initialized, false if not */ public function init(array $config = []): bool diff --git a/Engine/WincacheEngine.php b/Engine/WincacheEngine.php index 4936f1188..e9374b620 100644 --- a/Engine/WincacheEngine.php +++ b/Engine/WincacheEngine.php @@ -39,7 +39,7 @@ class WincacheEngine extends CacheEngine * * Called automatically by the cache frontend * - * @param array $config array of setting for the engine + * @param array $config array of setting for the engine * @return bool True if the engine has been successfully initialized, false if not */ public function init(array $config = []): bool From 15ffd28292153e3b8089bbcab35fc8159a639e01 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 12 Sep 2021 01:18:25 +0530 Subject: [PATCH 05/36] Fix docblock. --- Cache.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Cache.php b/Cache.php index fabf32a3a..56a9c34da 100644 --- a/Cache.php +++ b/Cache.php @@ -579,9 +579,8 @@ public static function enabled(): bool * the cache key is empty. Can be any callable type supported by your PHP. * @param string $config The cache configuration to use for this operation. * Defaults to default. - * @return mixed If the key is found: the cached data, false if the data - * missing/expired, or an error. If the key is not found: boolean of the - * success of the write + * @return mixed If the key is found: the cached data. + * If the key is not found the value returned by the callable. */ public static function remember(string $key, callable $callable, string $config = 'default') { From c3b63d7fe0bd0d277eec6f01bdd714d0157d1128 Mon Sep 17 00:00:00 2001 From: mscherer Date: Mon, 13 Sep 2021 10:54:59 +0200 Subject: [PATCH 06/36] Fix some grammar topics. --- Cache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cache.php b/Cache.php index 01675a0b3..e5f7f0eb2 100644 --- a/Cache.php +++ b/Cache.php @@ -85,7 +85,7 @@ class Cache ]; /** - * Flag for tracking whether or not caching is enabled. + * Flag for tracking whether caching is enabled. * * @var bool */ @@ -548,7 +548,7 @@ public static function disable(): void } /** - * Check whether or not caching is enabled. + * Check whether caching is enabled. * * @return bool */ From 6dfe11a829fea480605c31502f6e46a281cd4940 Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 17 Sep 2021 15:29:13 +0200 Subject: [PATCH 07/36] Fix up types in arrays of var def --- Cache.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cache.php b/Cache.php index e5f7f0eb2..e0ec4815e 100644 --- a/Cache.php +++ b/Cache.php @@ -71,7 +71,7 @@ class Cache * An array mapping URL schemes to fully qualified caching engine * class names. * - * @var array + * @var array * @psalm-var array */ protected static $_dsnClassMap = [ @@ -94,7 +94,7 @@ class Cache /** * Group to Config mapping * - * @var array + * @var array */ protected static $_groups = []; @@ -503,8 +503,8 @@ public static function clearGroup(string $group, string $config = 'default'): bo * $configs will equal to `['posts' => ['daily', 'weekly']]` * Calling this method will load all the configured engines. * - * @param string|null $group group name or null to retrieve all group mappings - * @return array map of group and all configuration that has the same group + * @param string|null $group Group name or null to retrieve all group mappings + * @return array Map of group and all configuration that has the same group * @throws \Cake\Cache\InvalidArgumentException */ public static function groupConfigs(?string $group = null): array From 88fcec0ecf19a0b678008e853d1443ded742b3f1 Mon Sep 17 00:00:00 2001 From: mscherer Date: Wed, 22 Sep 2021 17:00:51 +0200 Subject: [PATCH 08/36] Make docblock var definitions more concrete. --- Engine/MemcachedEngine.php | 2 +- Engine/WincacheEngine.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/MemcachedEngine.php b/Engine/MemcachedEngine.php index 227161513..9bdd3638d 100644 --- a/Engine/MemcachedEngine.php +++ b/Engine/MemcachedEngine.php @@ -82,7 +82,7 @@ class MemcachedEngine extends CacheEngine * * Memcached must be compiled with JSON and igbinary support to use these engines * - * @var array + * @var array */ protected $_serializers = []; diff --git a/Engine/WincacheEngine.php b/Engine/WincacheEngine.php index e9374b620..ca0388e4b 100644 --- a/Engine/WincacheEngine.php +++ b/Engine/WincacheEngine.php @@ -30,7 +30,7 @@ class WincacheEngine extends CacheEngine * Contains the compiled group names * (prefixed with the global configuration prefix) * - * @var array + * @var array */ protected $_compiledGroupNames = []; From e6e98bd958dd2c20dac504b9c99342ae84c1e765 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sun, 26 Sep 2021 02:10:05 +0200 Subject: [PATCH 09/36] Fix up doc typos or missing class links. --- Cache.php | 2 +- CacheRegistry.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cache.php b/Cache.php index 0c8d8d37f..412ad1f2d 100644 --- a/Cache.php +++ b/Cache.php @@ -33,7 +33,7 @@ * * ``` * Cache::config('shared', [ - * 'className' => Cake\Cache\Engine\ApcuEngine::class, + * 'className' => \Cake\Cache\Engine\ApcuEngine::class, * 'prefix' => 'my_app_' * ]); * ``` diff --git a/CacheRegistry.php b/CacheRegistry.php index c70835fc0..1c5aa4660 100644 --- a/CacheRegistry.php +++ b/CacheRegistry.php @@ -24,7 +24,7 @@ /** * An object registry for cache engines. * - * Used by Cake\Cache\Cache to load and manage cache engines. + * Used by {@link \Cake\Cache\Cache} to load and manage cache engines. * * @extends \Cake\Core\ObjectRegistry<\Cake\Cache\CacheEngine> */ From 61e8f5978f4f2b738bd4b1dcf78e030f5012f449 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sun, 26 Sep 2021 12:24:37 +0200 Subject: [PATCH 10/36] Revert one change. --- Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cache.php b/Cache.php index 412ad1f2d..0c8d8d37f 100644 --- a/Cache.php +++ b/Cache.php @@ -33,7 +33,7 @@ * * ``` * Cache::config('shared', [ - * 'className' => \Cake\Cache\Engine\ApcuEngine::class, + * 'className' => Cake\Cache\Engine\ApcuEngine::class, * 'prefix' => 'my_app_' * ]); * ``` From cd9b121e93ac30315b862efeb2f957e3450d862e Mon Sep 17 00:00:00 2001 From: Corey Taylor Date: Sun, 24 Oct 2021 15:02:24 -0500 Subject: [PATCH 11/36] Require php 7.4 for packages --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f78e504ab..385a303b0 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "source": "https://github.com/cakephp/cache" }, "require": { - "php": ">=7.2.0", + "php": ">=7.4.0", "cakephp/core": "^4.0", "psr/simple-cache": "^1.0.0" }, From 02064cc6e9b47a2295c4944afa8b540e75ffa935 Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 17 Dec 2021 19:58:04 +0530 Subject: [PATCH 12/36] Revert "Merge branch '4.x' into 4.next" This reverts commit de4eb4f06bb7a7b8512bb4d7ac939e4f63e9a578, reversing changes made to c2e3db0eef03b730cfc5c41eedf9fae64427e59b. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 385a303b0..f78e504ab 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "source": "https://github.com/cakephp/cache" }, "require": { - "php": ">=7.4.0", + "php": ">=7.2.0", "cakephp/core": "^4.0", "psr/simple-cache": "^1.0.0" }, From e546384e2a362c7cd9da12457fb0b9ef6b2a2434 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 18 Dec 2021 18:32:04 +0530 Subject: [PATCH 13/36] Revert "Revert "Merge branch '4.x' into 4.next"" This reverts commit 2a3c42e6f4a6e3235c089ff440c0920ddbfdfb24. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f78e504ab..385a303b0 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "source": "https://github.com/cakephp/cache" }, "require": { - "php": ">=7.2.0", + "php": ">=7.4.0", "cakephp/core": "^4.0", "psr/simple-cache": "^1.0.0" }, From 0bc1d264b47ae8df51f4c6680d89edf38ac5c5c1 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 26 Jan 2022 17:44:58 +0530 Subject: [PATCH 14/36] Allow v2 of psr/log and psr/simple-cache. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f78e504ab..001a8a9dc 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "require": { "php": ">=7.2.0", "cakephp/core": "^4.0", - "psr/simple-cache": "^1.0.0" + "psr/simple-cache": "^1.0 || ^2.0" }, "provide": { "psr/simple-cache-implementation": "^1.0.0" From 94a4fda939d218cacfbdd36609eecfe13141789b Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 3 Feb 2022 19:44:49 +0530 Subject: [PATCH 15/36] Fix errors reported by static analyzers --- Engine/MemcachedEngine.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Engine/MemcachedEngine.php b/Engine/MemcachedEngine.php index 9bdd3638d..b714f5056 100644 --- a/Engine/MemcachedEngine.php +++ b/Engine/MemcachedEngine.php @@ -134,11 +134,6 @@ public function init(array $config = []): bool $this->_config['servers'] = [$this->_config['servers']]; } - /** @psalm-suppress RedundantPropertyInitializationCheck */ - if (isset($this->_Memcached)) { - return true; - } - if ($this->_config['persistent']) { $this->_Memcached = new Memcached($this->_config['persistent']); } else { From 37997fb3ce2f03d61d8acbb9566ed7d7584229fc Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 24 Feb 2022 11:05:39 -0500 Subject: [PATCH 16/36] Fix deleteMany stopping after the first failure DeleteMany should be more of a best-effort vs stopping at the first missing key. Fixes #16347 --- CacheEngine.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CacheEngine.php b/CacheEngine.php index 8e9fc613a..14006deb5 100644 --- a/CacheEngine.php +++ b/CacheEngine.php @@ -198,14 +198,14 @@ public function deleteMultiple($keys): bool { $this->ensureValidType($keys); + $result = true; foreach ($keys as $key) { - $result = $this->delete($key); - if ($result === false) { - return false; + if (!$this->delete($key)) { + $result = false; } } - return true; + return $result; } /** From a5477023f7bdc2f35d0b9f138a7b8e66a6a13531 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 24 Feb 2022 22:25:10 -0500 Subject: [PATCH 17/36] Improve API docs for deleteMultiple --- CacheEngine.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CacheEngine.php b/CacheEngine.php index 14006deb5..ed01098e3 100644 --- a/CacheEngine.php +++ b/CacheEngine.php @@ -187,7 +187,11 @@ public function setMultiple($values, $ttl = null): bool } /** - * Deletes multiple cache items in a single operation. + * Deletes multiple cache items as a list + * + * This is a best effort attempt. If deleting an item would + * create an error it will be ignored, and all items will + * be attempted. * * @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. From 4ba3eaf201d25b5bf0b07ac6609cd2f98ad8b1c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Ram=C5=A1ak?= Date: Fri, 11 Mar 2022 10:38:03 +0100 Subject: [PATCH 18/36] Avoid possible TypeError Exception Exception: unlink(): Argument #1 ($filename) must be of type string, bool given In [/home/wotanabx/webseiten/wow-das/vendor/cakephp/cakephp/src/Cache/Engine/FileEngine.php, line 227] 2022-03-11 02:32:30 error: [TypeError] unlink(): Argument #1 ($filename) must be of type string, bool given in /home/wotanabx/webseiten/wow-das/vendor/cakephp/cakephp/src/Cache/Engine/FileEngine.php on line 227 --- Engine/FileEngine.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Engine/FileEngine.php b/Engine/FileEngine.php index e7ce5ea2c..1d508dd8c 100644 --- a/Engine/FileEngine.php +++ b/Engine/FileEngine.php @@ -222,6 +222,10 @@ public function delete($key): bool /** @psalm-suppress PossiblyNullReference */ $path = $this->_File->getRealPath(); $this->_File = null; + + if ($path === false) { + return false; + } // phpcs:disable return @unlink($path); From 778ed613b26da70cd1f7f208278e1a55f742fdf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Ram=C5=A1ak?= Date: Fri, 11 Mar 2022 11:12:35 +0100 Subject: [PATCH 19/36] removed whitespace on empty line --- Engine/FileEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/FileEngine.php b/Engine/FileEngine.php index 1d508dd8c..46a7f187a 100644 --- a/Engine/FileEngine.php +++ b/Engine/FileEngine.php @@ -222,7 +222,7 @@ public function delete($key): bool /** @psalm-suppress PossiblyNullReference */ $path = $this->_File->getRealPath(); $this->_File = null; - + if ($path === false) { return false; } From b4342712701917b2088de873fc02bad5a059f6d4 Mon Sep 17 00:00:00 2001 From: itosho Date: Mon, 2 May 2022 22:45:37 +0900 Subject: [PATCH 20/36] Add deleteAsync and clearBlocking method for improving performance --- Engine/RedisEngine.php | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Engine/RedisEngine.php b/Engine/RedisEngine.php index b7f0fec4b..63cd66d96 100644 --- a/Engine/RedisEngine.php +++ b/Engine/RedisEngine.php @@ -227,6 +227,21 @@ public function delete($key): bool return $this->_Redis->del($key) > 0; } + /** + * Delete a key from the cache asynchronously + * + * Just unlink a key from the cache. The actual removal will happen later asynchronously. + * + * @param string $key Identifier for the data + * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed + */ + public function deleteAsync(string $key): bool + { + $key = $this->_key($key); + + return $this->_Redis->unlink($key) > 0; + } + /** * Delete all keys from the cache * @@ -256,6 +271,37 @@ public function clear(): bool return $isAllDeleted; } + /** + * Delete all keys from the cache by a blocking operation + * + * Faster than clear() using unlink method. + * + * @return bool True if the cache was successfully cleared, false otherwise + */ + public function clearBlocking(): bool + { + $this->_Redis->setOption(Redis::OPT_SCAN, (string)Redis::SCAN_RETRY); + + $isAllDeleted = true; + $iterator = null; + $pattern = $this->_config['prefix'] . '*'; + + while (true) { + $keys = $this->_Redis->scan($iterator, $pattern); + + if ($keys === false) { + break; + } + + foreach ($keys as $key) { + $isDeleted = ($this->_Redis->unlink($key) > 0); + $isAllDeleted = $isAllDeleted && $isDeleted; + } + } + + return $isAllDeleted; + } + /** * Write data for key into cache if it doesn't exist already. * If it already exists, it fails and returns false. From 70c5bf50ba349c69bfbbac9c90a4bd3d260dc301 Mon Sep 17 00:00:00 2001 From: Nicos Panayides Date: Wed, 8 Jun 2022 16:08:02 +0300 Subject: [PATCH 21/36] Added count to scan to help speed up cache clear with a large number of keys. Default to 100. --- Engine/RedisEngine.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Engine/RedisEngine.php b/Engine/RedisEngine.php index 63cd66d96..cfb5dd074 100644 --- a/Engine/RedisEngine.php +++ b/Engine/RedisEngine.php @@ -47,6 +47,7 @@ class RedisEngine extends CacheEngine * - `port` port number to the Redis server. * - `prefix` Prefix appended to all entries. Good for when you need to share a keyspace * with either another cache config or another application. + * - `scan_count` Number of keys to ask for each scan (default: 100) * - `server` URL or IP to the Redis server host. * - `timeout` timeout in seconds (float). * - `unix_socket` Path to the unix socket file (default: false) @@ -65,6 +66,7 @@ class RedisEngine extends CacheEngine 'server' => '127.0.0.1', 'timeout' => 0, 'unix_socket' => false, + 'scan_count' => 100, ]; /** @@ -256,7 +258,7 @@ public function clear(): bool $pattern = $this->_config['prefix'] . '*'; while (true) { - $keys = $this->_Redis->scan($iterator, $pattern); + $keys = $this->_Redis->scan($iterator, $pattern, (int)$this->_config['scan_count']); if ($keys === false) { break; @@ -287,7 +289,7 @@ public function clearBlocking(): bool $pattern = $this->_config['prefix'] . '*'; while (true) { - $keys = $this->_Redis->scan($iterator, $pattern); + $keys = $this->_Redis->scan($iterator, $pattern, (int)$this->_config['scan_count']); if ($keys === false) { break; From 9ecfc88a17103df58881a31d54411264e347ac2f Mon Sep 17 00:00:00 2001 From: Nicos Panayides Date: Wed, 8 Jun 2022 17:19:26 +0300 Subject: [PATCH 22/36] Change the default to 10 to match previous behavior --- Engine/RedisEngine.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/RedisEngine.php b/Engine/RedisEngine.php index cfb5dd074..6c4ba8333 100644 --- a/Engine/RedisEngine.php +++ b/Engine/RedisEngine.php @@ -47,7 +47,7 @@ class RedisEngine extends CacheEngine * - `port` port number to the Redis server. * - `prefix` Prefix appended to all entries. Good for when you need to share a keyspace * with either another cache config or another application. - * - `scan_count` Number of keys to ask for each scan (default: 100) + * - `scan_count` Number of keys to ask for each scan (default: 10) * - `server` URL or IP to the Redis server host. * - `timeout` timeout in seconds (float). * - `unix_socket` Path to the unix socket file (default: false) @@ -66,7 +66,7 @@ class RedisEngine extends CacheEngine 'server' => '127.0.0.1', 'timeout' => 0, 'unix_socket' => false, - 'scan_count' => 100, + 'scan_count' => 10, ]; /** From 3b0754763275e522cd72ff63e0c6c279283580ea Mon Sep 17 00:00:00 2001 From: Nicos Panayides Date: Sat, 11 Jun 2022 17:44:35 +0300 Subject: [PATCH 23/36] Rename scan_count option to scanCount --- Engine/RedisEngine.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Engine/RedisEngine.php b/Engine/RedisEngine.php index 6c4ba8333..dc90ef72f 100644 --- a/Engine/RedisEngine.php +++ b/Engine/RedisEngine.php @@ -47,7 +47,7 @@ class RedisEngine extends CacheEngine * - `port` port number to the Redis server. * - `prefix` Prefix appended to all entries. Good for when you need to share a keyspace * with either another cache config or another application. - * - `scan_count` Number of keys to ask for each scan (default: 10) + * - `scanCount` Number of keys to ask for each scan (default: 10) * - `server` URL or IP to the Redis server host. * - `timeout` timeout in seconds (float). * - `unix_socket` Path to the unix socket file (default: false) @@ -66,7 +66,7 @@ class RedisEngine extends CacheEngine 'server' => '127.0.0.1', 'timeout' => 0, 'unix_socket' => false, - 'scan_count' => 10, + 'scanCount' => 10, ]; /** @@ -258,7 +258,7 @@ public function clear(): bool $pattern = $this->_config['prefix'] . '*'; while (true) { - $keys = $this->_Redis->scan($iterator, $pattern, (int)$this->_config['scan_count']); + $keys = $this->_Redis->scan($iterator, $pattern, (int)$this->_config['scanCount']); if ($keys === false) { break; @@ -289,7 +289,7 @@ public function clearBlocking(): bool $pattern = $this->_config['prefix'] . '*'; while (true) { - $keys = $this->_Redis->scan($iterator, $pattern, (int)$this->_config['scan_count']); + $keys = $this->_Redis->scan($iterator, $pattern, (int)$this->_config['scanCount']); if ($keys === false) { break; From cd8af4a5f214413607e2eca2188a25bc2f7e3f61 Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Mon, 13 Jun 2022 13:51:02 +0200 Subject: [PATCH 24/36] Allow the 'P1D' notation for DateInterval cache ttl. --- CacheEngine.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CacheEngine.php b/CacheEngine.php index ed01098e3..da5bcc719 100644 --- a/CacheEngine.php +++ b/CacheEngine.php @@ -18,6 +18,7 @@ use Cake\Core\InstanceConfigTrait; use DateInterval; +use DateTime; use Psr\SimpleCache\CacheInterface; /** @@ -383,7 +384,9 @@ protected function duration($ttl): int return $ttl; } if ($ttl instanceof DateInterval) { - return (int)$ttl->format('%s'); + return (int)DateTime::createFromFormat('U', '0') + ->add($ttl) + ->format('U'); } throw new InvalidArgumentException('TTL values must be one of null, int, \DateInterval'); From 2e57d69b2478ab880c4ca833344c55526e81273c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgaras=20Janu=C5=A1auskas?= Date: Mon, 11 Jul 2022 13:51:09 +0300 Subject: [PATCH 25/36] Allow special symbols in cache key using FileEngine --- Engine/FileEngine.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Engine/FileEngine.php b/Engine/FileEngine.php index 46a7f187a..e9c9cd644 100644 --- a/Engine/FileEngine.php +++ b/Engine/FileEngine.php @@ -17,7 +17,6 @@ namespace Cake\Cache\Engine; use Cake\Cache\CacheEngine; -use Cake\Cache\InvalidArgumentException; use CallbackFilterIterator; use Exception; use FilesystemIterator; @@ -441,14 +440,7 @@ protected function _key($key): string { $key = parent::_key($key); - if (preg_match('/[\/\\<>?:|*"]/', $key)) { - throw new InvalidArgumentException( - "Cache key `{$key}` contains invalid characters. " . - 'You cannot use /, \\, <, >, ?, :, |, *, or " in cache keys.' - ); - } - - return $key; + return rawurlencode($key); } /** From 078e5568ce3d0586f66aacae813f67943d091920 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Wed, 17 Aug 2022 12:21:45 +0200 Subject: [PATCH 26/36] Fix up assoc docblocks. (#16689) Fix up assoc docblocks. Co-authored-by: ADmad --- Engine/ArrayEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/ArrayEngine.php b/Engine/ArrayEngine.php index 9050537eb..4693c0242 100644 --- a/Engine/ArrayEngine.php +++ b/Engine/ArrayEngine.php @@ -35,7 +35,7 @@ class ArrayEngine extends CacheEngine * * Structured as [key => [exp => expiration, val => value]] * - * @var array + * @var array */ protected $data = []; From 7bd71a2d7cd171b42638686197ab9ca98f16fa5b Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 7 Oct 2022 22:10:13 +0530 Subject: [PATCH 27/36] Update psalm --- Engine/FileEngine.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Engine/FileEngine.php b/Engine/FileEngine.php index e9c9cd644..35252b98e 100644 --- a/Engine/FileEngine.php +++ b/Engine/FileEngine.php @@ -172,6 +172,7 @@ public function get($key, $default = null) /** @psalm-suppress PossiblyNullReference */ $this->_File->rewind(); $time = time(); + /** @psalm-suppress RiskyCast */ $cachetime = (int)$this->_File->current(); if ($cachetime < $time) { From bf4ab67dad8b5f66afa8fc98f2d5af9b068c1548 Mon Sep 17 00:00:00 2001 From: Brian French Date: Sat, 25 Feb 2023 15:00:46 -0500 Subject: [PATCH 28/36] Throw an Exception instead of an error on Cache::write() fail. See original #16932 (#17034) Throw a CacheWriteException when a Cache::write() fails, instead of triggering an error. This allows the developer more control over how to handle a Cache::write() issue. --- Cache.php | 31 ++++++++++++----------- CacheEngine.php | 19 +++++++------- Engine/MemcachedEngine.php | 6 ++--- Exception/CacheWriteException.php | 27 ++++++++++++++++++++ Exception/InvalidArgumentException.php | 34 ++++++++++++++++++++++++++ InvalidArgumentException.php | 28 +++++---------------- 6 files changed, 95 insertions(+), 50 deletions(-) create mode 100644 Exception/CacheWriteException.php create mode 100644 Exception/InvalidArgumentException.php diff --git a/Cache.php b/Cache.php index 0c8d8d37f..32ae10473 100644 --- a/Cache.php +++ b/Cache.php @@ -17,6 +17,8 @@ namespace Cake\Cache; use Cake\Cache\Engine\NullEngine; +use Cake\Cache\Exception\CacheWriteException; +use Cake\Cache\Exception\InvalidArgumentException; use Cake\Core\StaticConfigTrait; use RuntimeException; @@ -136,7 +138,7 @@ public static function setRegistry(CacheRegistry $registry): void * Finds and builds the instance of the required engine class. * * @param string $name Name of the config array that needs an engine instance built - * @throws \Cake\Cache\InvalidArgumentException When a cache engine cannot be created. + * @throws \Cake\Cache\Exception\InvalidArgumentException When a cache engine cannot be created. * @throws \RuntimeException If loading of the engine failed. * @return void */ @@ -265,15 +267,12 @@ public static function write(string $key, $value, string $config = 'default'): b $backend = static::pool($config); $success = $backend->set($key, $value); if ($success === false && $value !== '') { - trigger_error( - sprintf( - "%s cache was unable to write '%s' to %s cache", - $config, - $key, - get_class($backend) - ), - E_USER_WARNING - ); + throw new CacheWriteException(sprintf( + "%s cache was unable to write '%s' to %s cache", + $config, + $key, + get_class($backend) + )); } return $success; @@ -299,7 +298,7 @@ public static function write(string $key, $value, string $config = 'default'): b * @param iterable $data An array or Traversable of data to be stored in the cache * @param string $config Optional string configuration name to write to. Defaults to 'default' * @return bool True on success, false on failure - * @throws \Cake\Cache\InvalidArgumentException + * @throws \Cake\Cache\Exception\InvalidArgumentException */ public static function writeMany(iterable $data, string $config = 'default'): bool { @@ -354,7 +353,7 @@ public static function read(string $key, string $config = 'default') * @param string $config optional name of the configuration to use. Defaults to 'default' * @return iterable An array containing, for each of the given $keys, * the cached data or false if cached data could not be retrieved. - * @throws \Cake\Cache\InvalidArgumentException + * @throws \Cake\Cache\Exception\InvalidArgumentException */ public static function readMany(iterable $keys, string $config = 'default'): iterable { @@ -369,7 +368,7 @@ public static function readMany(iterable $keys, string $config = 'default'): ite * @param string $config Optional string configuration name. Defaults to 'default' * @return int|false New value, or false if the data doesn't exist, is not integer, * or if there was an error fetching it. - * @throws \Cake\Cache\InvalidArgumentException When offset < 0 + * @throws \Cake\Cache\Exception\InvalidArgumentException When offset < 0 */ public static function increment(string $key, int $offset = 1, string $config = 'default') { @@ -388,7 +387,7 @@ public static function increment(string $key, int $offset = 1, string $config = * @param string $config Optional string configuration name. Defaults to 'default' * @return int|false New value, or false if the data doesn't exist, is not integer, * or if there was an error fetching it - * @throws \Cake\Cache\InvalidArgumentException when offset < 0 + * @throws \Cake\Cache\Exception\InvalidArgumentException when offset < 0 */ public static function decrement(string $key, int $offset = 1, string $config = 'default') { @@ -445,7 +444,7 @@ public static function delete(string $key, string $config = 'default'): bool * @param iterable $keys Array or Traversable of cache keys to be deleted * @param string $config name of the configuration to use. Defaults to 'default' * @return bool True on success, false on failure. - * @throws \Cake\Cache\InvalidArgumentException + * @throws \Cake\Cache\Exception\InvalidArgumentException */ public static function deleteMany(iterable $keys, string $config = 'default'): bool { @@ -505,7 +504,7 @@ public static function clearGroup(string $group, string $config = 'default'): bo * * @param string|null $group Group name or null to retrieve all group mappings * @return array Map of group and all configuration that has the same group - * @throws \Cake\Cache\InvalidArgumentException + * @throws \Cake\Cache\Exception\InvalidArgumentException */ public static function groupConfigs(?string $group = null): array { diff --git a/CacheEngine.php b/CacheEngine.php index da5bcc719..683d0996c 100644 --- a/CacheEngine.php +++ b/CacheEngine.php @@ -16,6 +16,7 @@ */ namespace Cake\Cache; +use Cake\Cache\Exception\InvalidArgumentException; use Cake\Core\InstanceConfigTrait; use DateInterval; use DateTime; @@ -96,7 +97,7 @@ public function init(array $config = []): bool * * @param string $key Key to check. * @return void - * @throws \Cake\Cache\InvalidArgumentException When the key is not valid. + * @throws \Cake\Cache\Exception\InvalidArgumentException When the key is not valid. */ protected function ensureValidKey($key): void { @@ -111,7 +112,7 @@ protected function ensureValidKey($key): void * @param iterable $iterable The iterable to check. * @param string $check Whether to check keys or values. * @return void - * @throws \Cake\Cache\InvalidArgumentException + * @throws \Cake\Cache\Exception\InvalidArgumentException */ protected function ensureValidType($iterable, string $check = self::CHECK_VALUE): void { @@ -137,7 +138,7 @@ protected function ensureValidType($iterable, string $check = self::CHECK_VALUE) * @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 \Cake\Cache\InvalidArgumentException If $keys is neither an array nor a Traversable, + * @throws \Cake\Cache\Exception\InvalidArgumentException 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): iterable @@ -160,7 +161,7 @@ public function getMultiple($keys, $default = null): iterable * 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 \Cake\Cache\InvalidArgumentException If $values is neither an array nor a Traversable, + * @throws \Cake\Cache\Exception\InvalidArgumentException 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): bool @@ -196,7 +197,7 @@ public function setMultiple($values, $ttl = null): bool * * @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 \Cake\Cache\InvalidArgumentException If $keys is neither an array nor a Traversable, + * @throws \Cake\Cache\Exception\InvalidArgumentException If $keys is neither an array nor a Traversable, * or if any of the $keys are not a legal value. */ public function deleteMultiple($keys): bool @@ -223,7 +224,7 @@ public function deleteMultiple($keys): bool * * @param string $key The cache item key. * @return bool - * @throws \Cake\Cache\InvalidArgumentException If the $key string is not a legal value. + * @throws \Cake\Cache\Exception\InvalidArgumentException If the $key string is not a legal value. */ public function has($key): bool { @@ -236,7 +237,7 @@ public function has($key): bool * @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 \Cake\Cache\InvalidArgumentException If the $key string is not a legal value. + * @throws \Cake\Cache\Exception\InvalidArgumentException If the $key string is not a legal value. */ abstract public function get($key, $default = null); @@ -249,7 +250,7 @@ abstract public function get($key, $default = null); * 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 \Cake\Cache\InvalidArgumentException + * @throws \Cake\Cache\Exception\InvalidArgumentException * MUST be thrown if the $key string is not a legal value. */ abstract public function set($key, $value, $ttl = null): bool; @@ -337,7 +338,7 @@ public function groups(): array * * @param string $key the key passed over * @return string Prefixed key with potentially unsafe characters replaced. - * @throws \Cake\Cache\InvalidArgumentException If key's value is invalid. + * @throws \Cake\Cache\Exception\InvalidArgumentException If key's value is invalid. */ protected function _key($key): string { diff --git a/Engine/MemcachedEngine.php b/Engine/MemcachedEngine.php index b714f5056..f490b363e 100644 --- a/Engine/MemcachedEngine.php +++ b/Engine/MemcachedEngine.php @@ -17,7 +17,7 @@ namespace Cake\Cache\Engine; use Cake\Cache\CacheEngine; -use InvalidArgumentException; +use Cake\Cache\Exception\InvalidArgumentException; use Memcached; use RuntimeException; @@ -98,7 +98,7 @@ class MemcachedEngine extends CacheEngine * * @param array $config array of setting for the engine * @return bool True if the engine has been successfully initialized, false if not - * @throws \InvalidArgumentException When you try use authentication without + * @throws \Cake\Cache\Exception\InvalidArgumentException When you try use authentication without * Memcached compiled with SASL support */ public function init(array $config = []): bool @@ -199,7 +199,7 @@ public function init(array $config = []): bool * Settings the memcached instance * * @return void - * @throws \InvalidArgumentException When the Memcached extension is not built + * @throws \Cake\Cache\Exception\InvalidArgumentException When the Memcached extension is not built * with the desired serializer engine. */ protected function _setOptions(): void diff --git a/Exception/CacheWriteException.php b/Exception/CacheWriteException.php new file mode 100644 index 000000000..241ef6539 --- /dev/null +++ b/Exception/CacheWriteException.php @@ -0,0 +1,27 @@ + Date: Wed, 8 Mar 2023 10:56:37 +0530 Subject: [PATCH 29/36] Update "provide" config of various packages. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 96aec1982..7f20c1e9a 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "psr/simple-cache": "^1.0 || ^2.0" }, "provide": { - "psr/simple-cache-implementation": "^1.0.0" + "psr/simple-cache-implementation": "^1.0 || ^2.0" }, "autoload": { "psr-4": { From 0c63de0c3cfe828e5953446f6c496c647cfa537f Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 10 Mar 2023 12:15:58 +0530 Subject: [PATCH 30/36] Namespace all functions. Add global versions if not already defined. --- Cache.php | 1 + CacheEngine.php | 1 + InvalidArgumentException.php | 2 ++ 3 files changed, 4 insertions(+) diff --git a/Cache.php b/Cache.php index 32ae10473..b91032291 100644 --- a/Cache.php +++ b/Cache.php @@ -21,6 +21,7 @@ use Cake\Cache\Exception\InvalidArgumentException; use Cake\Core\StaticConfigTrait; use RuntimeException; +use function Cake\Core\deprecationWarning; /** * Cache provides a consistent interface to Caching in your application. It allows you diff --git a/CacheEngine.php b/CacheEngine.php index 683d0996c..b37fc672f 100644 --- a/CacheEngine.php +++ b/CacheEngine.php @@ -21,6 +21,7 @@ use DateInterval; use DateTime; use Psr\SimpleCache\CacheInterface; +use function Cake\Core\triggerWarning; /** * Storage engine for CakePHP caching diff --git a/InvalidArgumentException.php b/InvalidArgumentException.php index 8d90447d2..8d0e9e6a5 100644 --- a/InvalidArgumentException.php +++ b/InvalidArgumentException.php @@ -1,6 +1,8 @@ Date: Tue, 4 Apr 2023 12:40:19 +0200 Subject: [PATCH 31/36] Use 0770 as default perms. --- Engine/FileEngine.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/FileEngine.php b/Engine/FileEngine.php index 35252b98e..a7137dcba 100644 --- a/Engine/FileEngine.php +++ b/Engine/FileEngine.php @@ -371,7 +371,7 @@ protected function _setKey(string $key, bool $createKey = false): bool $dir = $this->_config['path'] . $groups; if (!is_dir($dir)) { - mkdir($dir, 0775, true); + mkdir($dir, 0770, true); } $path = new SplFileInfo($dir . $key); @@ -418,7 +418,7 @@ protected function _active(): bool $success = true; if (!is_dir($path)) { // phpcs:disable - $success = @mkdir($path, 0775, true); + $success = @mkdir($path, 0770, true); // phpcs:enable } From 01ca89cf0935bac534adf5592fe8708746944b05 Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 4 Apr 2023 13:05:01 +0200 Subject: [PATCH 32/36] Use dirMask option to configure the folder perms. --- Engine/FileEngine.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Engine/FileEngine.php b/Engine/FileEngine.php index a7137dcba..9e786787a 100644 --- a/Engine/FileEngine.php +++ b/Engine/FileEngine.php @@ -50,6 +50,7 @@ class FileEngine extends CacheEngine * handy for deleting a complete group from cache. * - `lock` Used by FileCache. Should files be locked before writing to them? * - `mask` The mask used for created files + * - `dirMask` The mask used for created folders * - `path` Path to where cachefiles should be saved. Defaults to system's temp dir. * - `prefix` Prepended to all entries. Good for when you need to share a keyspace * with either another cache config or another application. @@ -63,6 +64,7 @@ class FileEngine extends CacheEngine 'groups' => [], 'lock' => true, 'mask' => 0664, + 'dirMask' => 0770, 'path' => null, 'prefix' => 'cake_', 'serialize' => true, @@ -371,7 +373,7 @@ protected function _setKey(string $key, bool $createKey = false): bool $dir = $this->_config['path'] . $groups; if (!is_dir($dir)) { - mkdir($dir, 0770, true); + mkdir($dir, $this->_config['dirMask'], true); } $path = new SplFileInfo($dir . $key); @@ -418,7 +420,7 @@ protected function _active(): bool $success = true; if (!is_dir($path)) { // phpcs:disable - $success = @mkdir($path, 0770, true); + $success = @mkdir($path, $this->_config['dirMask'], true); // phpcs:enable } From 3af53307fcac61103a5d2194dffa6d522ef39588 Mon Sep 17 00:00:00 2001 From: "Frank de Graaf (Phally)" Date: Fri, 19 May 2023 21:18:04 +0200 Subject: [PATCH 33/36] Adds TLS support to RedisEngine. TLS support was added in php-redis v5.3.0. This adds support for `'tls' => true` in the engine configuration and using `tls=true` in the DSN configuration. Additionally, support was added for SSL context options. In the DSN one can now use `ssl_ca`, `ssl_key`, and `ssl_cert` as well, similar to the MySQL database driver. Closes #17130 --- Engine/RedisEngine.php | 104 +++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 13 deletions(-) diff --git a/Engine/RedisEngine.php b/Engine/RedisEngine.php index dc90ef72f..cdd479fb2 100644 --- a/Engine/RedisEngine.php +++ b/Engine/RedisEngine.php @@ -45,6 +45,7 @@ class RedisEngine extends CacheEngine * - `password` Redis server password. * - `persistent` Connect to the Redis server with a persistent connection * - `port` port number to the Redis server. + * - `tls` connect to the Redis server using TLS. * - `prefix` Prefix appended to all entries. Good for when you need to share a keyspace * with either another cache config or another application. * - `scanCount` Number of keys to ask for each scan (default: 10) @@ -61,6 +62,7 @@ class RedisEngine extends CacheEngine 'password' => false, 'persistent' => true, 'port' => 6379, + 'tls' => false, 'prefix' => 'cake_', 'host' => null, 'server' => '127.0.0.1', @@ -99,24 +101,29 @@ public function init(array $config = []): bool */ protected function _connect(): bool { + $tls = $this->_config['tls'] === true ? 'tls://' : ''; + + $map = [ + 'ssl_ca' => 'cafile', + 'ssl_key' => 'local_pk', + 'ssl_cert' => 'local_cert', + ]; + + $ssl = []; + foreach ($map as $key => $context) { + if (!empty($this->_config[$key])) { + $ssl[$context] = $this->_config[$key]; + } + } + try { - $this->_Redis = new Redis(); + $this->_Redis = $this->_createRedisInstance(); if (!empty($this->_config['unix_socket'])) { $return = $this->_Redis->connect($this->_config['unix_socket']); } elseif (empty($this->_config['persistent'])) { - $return = $this->_Redis->connect( - $this->_config['server'], - (int)$this->_config['port'], - (int)$this->_config['timeout'] - ); + $return = $this->_connectTransient($tls . $this->_config['server'], $ssl); } else { - $persistentId = $this->_config['port'] . $this->_config['timeout'] . $this->_config['database']; - $return = $this->_Redis->pconnect( - $this->_config['server'], - (int)$this->_config['port'], - (int)$this->_config['timeout'], - $persistentId - ); + $return = $this->_connectPersistent($tls . $this->_config['server'], $ssl); } } catch (RedisException $e) { if (class_exists(Log::class)) { @@ -135,6 +142,67 @@ protected function _connect(): bool return $return; } + /** + * Connects to a Redis server using a new connection. + * + * @param string $server Server to connect to. + * @param array $ssl SSL context options. + * @throws \RedisException + * @return bool True if Redis server was connected + */ + protected function _connectTransient($server, array $ssl): bool + { + if (empty($ssl)) { + return $this->_Redis->connect( + $server, + (int)$this->_config['port'], + (int)$this->_config['timeout'] + ); + } + + return $this->_Redis->connect( + $server, + (int)$this->_config['port'], + (int)$this->_config['timeout'], + null, + 0, + 0.0, + ['ssl' => $ssl] + ); + } + + /** + * Connects to a Redis server using a persistent connection. + * + * @param string $server Server to connect to. + * @param array $ssl SSL context options. + * @throws \RedisException + * @return bool True if Redis server was connected + */ + protected function _connectPersistent($server, array $ssl): bool + { + $persistentId = $this->_config['port'] . $this->_config['timeout'] . $this->_config['database']; + + if (empty($ssl)) { + return $this->_Redis->pconnect( + $server, + (int)$this->_config['port'], + (int)$this->_config['timeout'], + $persistentId + ); + } + + return $this->_Redis->pconnect( + $server, + (int)$this->_config['port'], + (int)$this->_config['timeout'], + $persistentId, + 0, + 0.0, + ['ssl' => $ssl] + ); + } + /** * Write data for key into cache. * @@ -394,6 +462,16 @@ protected function unserialize(string $value) return unserialize($value); } + /** + * Create new Redis instance. + * + * @return \Redis + */ + protected function _createRedisInstance(): Redis + { + return new Redis(); + } + /** * Disconnects from the redis server */ From ee026fee997270e6f1b6e084210cec57570c3990 Mon Sep 17 00:00:00 2001 From: Joris Vaesen Date: Tue, 30 Apr 2024 11:54:26 +0200 Subject: [PATCH 34/36] Update MemcachedEngine to handle empty prefix --- Engine/MemcachedEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/MemcachedEngine.php b/Engine/MemcachedEngine.php index f490b363e..29c56954a 100644 --- a/Engine/MemcachedEngine.php +++ b/Engine/MemcachedEngine.php @@ -437,7 +437,7 @@ public function clear(): bool } foreach ($keys as $key) { - if (strpos($key, $this->_config['prefix']) === 0) { + if ($this->_config['prefix'] === '' || strpos($key, $this->_config['prefix']) === 0) { $this->_Memcached->delete($key); } } From 9b2683b20bd111a6f6dc2eaa3ac40a2f94a3f36d Mon Sep 17 00:00:00 2001 From: Joris Vaesen Date: Mon, 24 Mar 2025 15:13:53 +0100 Subject: [PATCH 35/36] Make NullEngine return the original keys with the default as other cache engines do --- Engine/NullEngine.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Engine/NullEngine.php b/Engine/NullEngine.php index 4612a276b..89defe467 100644 --- a/Engine/NullEngine.php +++ b/Engine/NullEngine.php @@ -62,7 +62,13 @@ public function get($key, $default = null) */ public function getMultiple($keys, $default = null): iterable { - return []; + $result = []; + + foreach ($keys as $key) { + $result[$key] = $default; + } + + return $result; } /** From 701b8a547f0a2556666567f3bb4efe4d17f7fa02 Mon Sep 17 00:00:00 2001 From: Joris Vaesen Date: Sat, 29 Mar 2025 18:03:24 +0100 Subject: [PATCH 36/36] Backport: Treat null as a valid cache value on MemcachedEngine::getMultiple (#18254) Treat null as a valid cache value on MemcachedEngine::getMultiple --- Engine/MemcachedEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/MemcachedEngine.php b/Engine/MemcachedEngine.php index 29c56954a..687987603 100644 --- a/Engine/MemcachedEngine.php +++ b/Engine/MemcachedEngine.php @@ -365,7 +365,7 @@ public function getMultiple($keys, $default = null): array $values = $this->_Memcached->getMulti($cacheKeys); $return = []; foreach ($cacheKeys as $original => $prefixed) { - $return[$original] = $values[$prefixed] ?? $default; + $return[$original] = array_key_exists($prefixed, $values) ? $values[$prefixed] : $default; } return $return;