From 471cf4f9527cc0d18ccf06e2e2653033d0f506f6 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 1 Mar 2021 15:48:14 +0100 Subject: [PATCH 1/4] requires PHP 8.0 --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6162d308..c8541666 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ ], "require": { "php": ">=8.0 <8.3", - "nette/utils": "^4.0" + "nette/finder": "^2.4 || ^3.0", + "nette/utils": "^2.4 || ^3.0" }, "require-dev": { "nette/tester": "^2.4", From c534590ab43a11fad08a4a174b5b3b4b64647182 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 1 Mar 2021 16:44:27 +0100 Subject: [PATCH 2/4] composer: updated dependencies --- composer.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index c8541666..8e5b9f92 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,11 @@ ], "require": { "php": ">=8.0 <8.3", - "nette/finder": "^2.4 || ^3.0", - "nette/utils": "^2.4 || ^3.0" + "nette/utils": "^4.0" }, "require-dev": { "nette/tester": "^2.4", - "nette/di": "^3.1 || ^4.0", + "nette/di": "^v4.0", "latte/latte": "^2.11 || ^3.0", "tracy/tracy": "^2.8", "phpstan/phpstan": "^1.0" From 1a1327f982fbdb37ab6594ea898510c14b959473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Mon, 9 Jan 2023 11:16:51 +0100 Subject: [PATCH 3/4] typo --- src/Caching/Cache.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Caching/Cache.php b/src/Caching/Cache.php index f5534081..49105f99 100644 --- a/src/Caching/Cache.php +++ b/src/Caching/Cache.php @@ -179,14 +179,13 @@ public function bulkLoad(array $keys, ?callable $generator = null): array /** * Writes item into the cache. * Dependencies are: - * - Cache::Priortiy => (int) priority - * - Cache::Exprie => (timestamp) expiration + * - Cache::Priority => (int) priority + * - Cache::Expire => (timestamp) expiration * - Cache::Sliding => (bool) use sliding expiration? * - Cache::Tags => (array) tags * - Cache::Files => (array|string) file names * - Cache::Items => (array|string) cache items - * - Cache::Consts => (array|string) cache items - * + * - Cache::Constants => (array|string) cache items * @return mixed value itself * @throws Nette\InvalidArgumentException */ From d9089d7cbab0cdb9e4a83f2411c38dae5b85dc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Mon, 9 Jan 2023 11:36:16 +0100 Subject: [PATCH 4/4] added $dependencies as a Cache::load() 3rd parameter --- readme.md | 8 ++++++++ src/Caching/Cache.php | 2 +- tests/Caching/Cache.load.phpt | 13 +++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 2df59f89..78b83b2b 100644 --- a/readme.md +++ b/readme.md @@ -131,6 +131,14 @@ $value = $cache->load($key, function (&$dependencies) { ]); ``` +Or using the `$dependencies` as a 3rd parameter in the `load()` method, eg: + +```php +$value = $cache->load($key, function () { + return ...; +], [Cache::Expire => '20 minutes']); +``` + In the following examples, we will assume the second variant and thus the existence of a variable `$dependencies`. diff --git a/src/Caching/Cache.php b/src/Caching/Cache.php index 49105f99..335e5a84 100644 --- a/src/Caching/Cache.php +++ b/src/Caching/Cache.php @@ -110,7 +110,7 @@ public function derive(string $namespace): static /** * Reads the specified item from the cache or generate it. */ - public function load(mixed $key, ?callable $generator = null): mixed + public function load(mixed $key, ?callable $generator = null, ?array $dependencies = null): mixed { $storageKey = $this->generateKey($key); $data = $this->storage->read($storageKey); diff --git a/tests/Caching/Cache.load.phpt b/tests/Caching/Cache.load.phpt index 581da666..f64e7e0a 100644 --- a/tests/Caching/Cache.load.phpt +++ b/tests/Caching/Cache.load.phpt @@ -42,6 +42,15 @@ Assert::same('value', $data['data']); Assert::same($dependencies, $data['dependencies']); +$value = $cache->load('key2', fn() => 'value2', $dependencies); +Assert::same('value2', $value); + +$data = $cache->load('key2', fn() => "won't load this value"); +Assert::same('value2', $data['data']); +Assert::same($dependencies, $data['dependencies']); + + + // load twice with fallback, pass dependencies function fallback(&$deps) { @@ -50,8 +59,8 @@ function fallback(&$deps) } -$value = $cache->load('key2', 'fallback'); +$value = $cache->load('key3', 'fallback'); Assert::same('value', $value); -$data = $cache->load('key2'); +$data = $cache->load('key3'); Assert::same('value', $data['data']); Assert::same($dependencies, $data['dependencies']);