MediaWiki  master
MemoizedCallable Class Reference

APC-backed and APCu-backed function memoization. More...

Public Member Functions

 __construct ( $callable, $ttl=3600)
 
 invoke (... $params)
 Invoke the memoized function or method. More...
 
 invokeArgs (array $args=[])
 Invoke the memoized function or method. More...
 

Static Public Member Functions

static call ( $callable, array $args=[], $ttl=3600)
 Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments. More...
 

Protected Member Functions

 fetchResult ( $key, &$success)
 Fetch the result of a previous invocation from APC or APCu. More...
 
 storeResult ( $key, $result)
 Store the result of an invocation in APC or APCu. More...
 

Private Attributes

callable $callable
 
string $callableName
 Unique name of callable; used for cache keys. More...
 
int $ttl
 

Detailed Description

APC-backed and APCu-backed function memoization.

This class provides memoization for pure functions. A function is pure if its result value depends on nothing other than its input parameters and if invoking it does not cause any side-effects.

The first invocation of the memoized callable with a particular set of arguments will be delegated to the underlying callable. Repeat invocations with the same input parameters will be served from APC or APCu.

Example:
$memoizedStrrev = new MemoizedCallable( 'range' );
$memoizedStrrev->invoke( 5, 8 ); // result: array( 5, 6, 7, 8 )
$memoizedStrrev->invokeArgs( array( 5, 8 ) ); // same
MemoizedCallable::call( 'range', array( 5, 8 ) ); // same
Since
1.27

Definition at line 43 of file MemoizedCallable.php.

Constructor & Destructor Documentation

◆ __construct()

MemoizedCallable::__construct (   $callable,
  $ttl = 3600 
)
Exceptions
InvalidArgumentExceptionif $callable is not a callable.
Parameters
callable$callableFunction or method to memoize.
int$ttlTTL in seconds. Defaults to 3600 (1hr). Capped at 86400 (24h).

Definition at line 59 of file MemoizedCallable.php.

References $callable, and $ttl.

Member Function Documentation

◆ call()

static MemoizedCallable::call (   $callable,
array  $args = [],
  $ttl = 3600 
)
static

Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments.

Parameters
callable$callable
array$args
int$ttl
Returns
mixed

Definition at line 157 of file MemoizedCallable.php.

References $args, $callable, and $ttl.

Referenced by ResourceLoaderWikiModule\getStyles().

◆ fetchResult()

MemoizedCallable::fetchResult (   $key,
$success 
)
protected

Fetch the result of a previous invocation from APC or APCu.

Parameters
string$key
bool&$success
Returns
bool

Definition at line 83 of file MemoizedCallable.php.

References $success.

Referenced by invokeArgs().

◆ invoke()

MemoizedCallable::invoke (   $params)

Invoke the memoized function or method.

Like MemoizedCallable::invokeArgs(), but variadic.

Parameters
mixed...$params Parameters for memoized function or method.
Returns
mixed The memoized callable's return value.

Definition at line 144 of file MemoizedCallable.php.

References invokeArgs().

◆ invokeArgs()

MemoizedCallable::invokeArgs ( array  $args = [])

Invoke the memoized function or method.

Exceptions
InvalidArgumentExceptionIf parameters list contains non-scalar items.
Parameters
array$argsParameters for memoized function or method.
Returns
mixed The memoized callable's return value.

Definition at line 114 of file MemoizedCallable.php.

References $args, $callable, $success, fetchResult(), serialize(), and storeResult().

Referenced by invoke().

◆ storeResult()

MemoizedCallable::storeResult (   $key,
  $result 
)
protected

Store the result of an invocation in APC or APCu.

Parameters
string$key
mixed$result

Definition at line 99 of file MemoizedCallable.php.

Referenced by invokeArgs().

Member Data Documentation

◆ $callable

callable MemoizedCallable::$callable
private

Definition at line 46 of file MemoizedCallable.php.

Referenced by __construct(), call(), and invokeArgs().

◆ $callableName

string MemoizedCallable::$callableName
private

Unique name of callable; used for cache keys.

Definition at line 49 of file MemoizedCallable.php.

◆ $ttl

int MemoizedCallable::$ttl
private

Definition at line 52 of file MemoizedCallable.php.

Referenced by __construct(), and call().


The documentation for this class was generated from the following file:
MemoizedCallable
APC-backed and APCu-backed function memoization.
Definition: MemoizedCallable.php:43
MemoizedCallable\call
static call( $callable, array $args=[], $ttl=3600)
Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments.
Definition: MemoizedCallable.php:157