Trait for caching decorators. For usage see https://www.schmengler-se.de/en/2016/09/memoize-method-calls-in-php-with-cache-decorators/
<?php | |
trait Memoize | |
{ | |
/** | |
* @var array [method][parameters] | |
*/ | |
private $memoizedResults = []; | |
protected function memoizedCall($methodName, $args) | |
{ | |
$serializedArgs = \serialize($args); | |
if (! isset($this->memoizedResults[$methodName][$serializedArgs])) { | |
$this->memoizedResults[$methodName][$serializedArgs] = | |
$this->subject->$methodName(...$args); | |
} | |
return $this->memoizedResults[$methodName][$serializedArgs]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment