Skip to content

Commit

Permalink
Catch exceptions in fetch()
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <[email protected]>
  • Loading branch information
betterthanclay committed Feb 29, 2024
1 parent e34554e commit 6c623a9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* Catch exceptions in fetch()
* Added unlock to items

## v0.3.0 (2023-11-14)
* Renamed enums to PascalCase

Expand Down
15 changes: 15 additions & 0 deletions src/Stash/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,21 @@ public function lock(
);
}

/**
* Remove lock entry
*/
public function unlock(): void
{
if (!$this->locked) {
return;
}

$this->store->getDriver()->deleteLock(
$this->store->getNamespace(),
$this->key
);
}

/**
* Store item to driver
*/
Expand Down
10 changes: 9 additions & 1 deletion src/Stash/Store/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use Psr\Cache\CacheItemInterface as CacheItem;
use Psr\Cache\InvalidArgumentException as CacheInvalidArgumentException;
use Throwable;

class Generic implements Store
{
Expand Down Expand Up @@ -294,7 +295,14 @@ public function fetch(
$item->isMiss()
) {
$item->lock();
$value = $generator($item, $this);

try {
$value = $generator($item, $this);
} catch (Throwable $e) {
$item->unlock();
throw $e;
}

$item->set($value);
$item->save();
}
Expand Down

0 comments on commit 6c623a9

Please sign in to comment.