Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"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"
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.


Expand Down
9 changes: 4 additions & 5 deletions src/Caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down
13 changes: 11 additions & 2 deletions tests/Caching/Cache.load.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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']);