Skip to content

Add StaticCache::remember() #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2025
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
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ edb78e6307a371aff090127ce28aae9369605af8
d4b03b64fcfc69e247b12f5f4afa4bd5f90ce5ab
aa3797614d9280deb1b174d4878d484f2695efec
e224e927dabcd3046455985118ed424c43aba054
20e2de4b44af7511cb0eb6e6f71676253b4c2667
14 changes: 11 additions & 3 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/Objects/Support/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EncoreDigitalGroup\StdLib\Objects\Support;

use BackedEnum;
use Closure;
use EncoreDigitalGroup\StdLib\Objects\Support\Traits\StaticCacheEnabled;

class StaticCache
Expand Down Expand Up @@ -66,6 +67,24 @@ public static function flush(BackedEnum|string|null $partition = null): void
}
}

public static function remember(BackedEnum|string $key, Closure $value, BackedEnum|string $partition = "default"): mixed
{
if (self::disabled()) {
return $value();
}

$partition = self::enum($partition);

if (self::contains($key, $partition)) {
return self::get($key, $partition);
}

$result = $value();
self::add($key, $result, $partition);

return $result;
}

protected static function enum(BackedEnum|string $name): string
{
if ($name instanceof BackedEnum) {
Expand Down