Skip to content

Commit

Permalink
memoize all
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko committed Dec 19, 2024
1 parent 0eefd0b commit c9ad28d
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/Singletons/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@
use App\Models\Tag;
use App\Traits\Memoizes;
use App\Transformers\TagTransformer;
use Illuminate\Support\Collection;

class Tags
{
use Memoizes;

/**
* @return Collection<Tag>
*/
public function all(): Collection
{
return $this->memoize(__FUNCTION__, fn () => Tag::all());
}

public function get(int $id): ?Tag
{
$allById = $this->memoize(
'allById',
fn () => Tag::all()->keyBy('id'),
fn () => $this->all()->keyBy('id'),
);

return $allById[$id] ?? null;
Expand All @@ -29,7 +38,7 @@ public function json(): array
{
return $this->memoize(
__FUNCTION__,
fn () => json_collection(Tag::all(), new TagTransformer()),
fn () => json_collection($this->all(), new TagTransformer()),
);
}
}

0 comments on commit c9ad28d

Please sign in to comment.