codingpaws / simple-memoize
Simple trait-based memoization to cache return values
1.2
2022-05-27 07:50 UTC
Requires (Dev)
- phpunit/phpunit: ^9.5
Suggests
- laravel/framework: >=8.0
This package is auto-updated.
Last update: 2024-04-27 11:59:57 UTC
README
codingpaws/simple-memoize
is a PHP library for simple, trait-based
memoization (i.e. caching return values per class). Although many PHP
memoization dependencies exist, there is none that can be used very
easily like this:
use CodingPaws\SimpleMemoize\Memoize;
class Calculator
{
use Memoize;
public function __construct(public int $base) {
}
public function pow10(): int
{
return $this->memoize(fn () => pow(10, $this->exponent));
}
}
$calculator = new Calculator(1234);
$calculator->pow10(); // 3.0913151596972
$calculator->pow10(); // will not be calculated again