Skip to content

Commit

Permalink
added $dependencies as a Cache::load() 3rd parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
milo authored and dg committed Jan 13, 2023
1 parent fc4066c commit 91a2e95
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions readme.md
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
2 changes: 1 addition & 1 deletion src/Caching/Cache.php
Expand Up @@ -109,7 +109,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
13 changes: 11 additions & 2 deletions tests/Caching/Cache.load.phpt
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']);

0 comments on commit 91a2e95

Please sign in to comment.