3 * APC-backed and APCu-backed function memoization
5 * This class provides memoization for pure functions. A function is pure
6 * if its result value depends on nothing other than its input parameters
7 * and if invoking it does not cause any side-effects.
9 * The first invocation of the memoized callable with a particular set of
10 * arguments will be delegated to the underlying callable. Repeat invocations
11 * with the same input parameters will be served from APC or APCu.
15 * $memoizedStrrev = new MemoizedCallable( 'range' );
16 * $memoizedStrrev->invoke( 5, 8 ); // result: array( 5, 6, 7, 8 )
17 * $memoizedStrrev->invokeArgs( array( 5, 8 ) ); // same
18 * MemoizedCallable::call( 'range', array( 5, 8 ) ); // same
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 * http://www.gnu.org/copyleft/gpl.html
40 class MemoizedCallable
{
45 /** @var string Unique name of callable; used for cache keys. */
46 private $callableName;
49 * @throws InvalidArgumentException if $callable is not a callable.
50 * @param callable $callable Function or method to memoize.
51 * @param int $ttl TTL in seconds. Defaults to 3600 (1hr). Capped at 86400 (24h).
53 public function __construct( $callable, $ttl = 3600 ) {
54 if ( !is_callable( $callable, false, $this->callableName
) ) {
55 throw new InvalidArgumentException(
56 'Argument 1 passed to MemoizedCallable::__construct() must ' .
57 'be an instance of callable; ' . gettype( $callable ) . ' given'
61 if ( $this->callableName
=== 'Closure::__invoke' ) {
62 // Differentiate anonymous functions from one another
63 $this->callableName
.= uniqid();
66 $this->callable
= $callable;
67 $this->ttl
= min( max( $ttl, 1 ), 86400 );
71 * Fetch the result of a previous invocation from APC or APCu.
74 * @param bool &$success
76 protected function fetchResult( $key, &$success ) {
78 if ( function_exists( 'apc_fetch' ) ) {
79 return apc_fetch( $key, $success );
80 } elseif ( function_exists( 'apcu_fetch' ) ) {
81 return apcu_fetch( $key, $success );
87 * Store the result of an invocation in APC or APCu.
90 * @param mixed $result
92 protected function storeResult( $key, $result ) {
93 if ( function_exists( 'apc_store' ) ) {
94 apc_store( $key, $result, $this->ttl
);
95 } elseif ( function_exists( 'apcu_store' ) ) {
96 apcu_store( $key, $result, $this->ttl
);
101 * Invoke the memoized function or method.
103 * @throws InvalidArgumentException If parameters list contains non-scalar items.
104 * @param array $args Parameters for memoized function or method.
105 * @return mixed The memoized callable's return value.
107 public function invokeArgs( array $args = [] ) {
108 foreach ( $args as $arg ) {
109 if ( $arg !== null && !is_scalar( $arg ) ) {
110 throw new InvalidArgumentException(
111 'MemoizedCallable::invoke() called with non-scalar ' .
117 $hash = md5( serialize( $args ) );
118 $key = __CLASS__
. ':' . $this->callableName
. ':' . $hash;
120 $result = $this->fetchResult( $key, $success );
122 $result = call_user_func_array( $this->callable
, $args );
123 $this->storeResult( $key, $result );
130 * Invoke the memoized function or method.
132 * Like MemoizedCallable::invokeArgs(), but variadic.
134 * @param mixed ...$params Parameters for memoized function or method.
135 * @return mixed The memoized callable's return value.
137 public function invoke() {
138 return $this->invokeArgs( func_get_args() );
142 * Shortcut method for creating a MemoizedCallable and invoking it
143 * with the specified arguments.
145 * @param callable $callable
149 public static function call( $callable, array $args = [], $ttl = 3600 ) {
150 $instance = new self( $callable, $ttl );
151 return $instance->invokeArgs( $args );