Trait for caching decorators. For usage see https://www.schmengler-se.de/en/2016/09/memoize-method-calls-in-php-with-cache-decorators/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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