49 private $callableName;
59 if ( !is_callable( $callable,
false, $this->callableName ) ) {
60 throw new InvalidArgumentException(
61 'Argument 1 passed to MemoizedCallable::__construct() must ' .
62 'be an instance of callable; ' . gettype( $callable ) .
' given'
66 if ( $this->callableName ===
'Closure::__invoke' ) {
67 throw new InvalidArgumentException(
'Cannot memoize unnamed closure' );
70 $this->callable = $callable;
71 $this->ttl = min( max( $ttl, 1 ), 86400 );
83 if ( function_exists(
'apcu_fetch' ) ) {
96 if ( function_exists(
'apcu_store' ) ) {
97 apcu_store( $key, $result, $this->ttl );
109 foreach (
$args as $arg ) {
110 if ( $arg !==
null && !is_scalar( $arg ) ) {
111 throw new InvalidArgumentException(
112 'MemoizedCallable::invoke() called with non-scalar ' .
119 $key = __CLASS__ .
':' . $this->callableName .
':' . $hash;
123 $result = ( $this->callable )( ...
$args );
151 public static function call( $callable, array
$args = [], $ttl = 3600 ) {
152 $instance =
new self( $callable, $ttl );
153 return $instance->invokeArgs(
$args );
APCu-backed function memoization.
static call( $callable, array $args=[], $ttl=3600)
Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments.
invoke(... $params)
Invoke the memoized function or method.
__construct( $callable, $ttl=3600)
fetchResult( $key, &$success)
Fetch the result of a previous invocation.
invokeArgs(array $args=[])
Invoke the memoized function or method.
storeResult( $key, $result)
Store the result of an invocation.