3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
23 * APC-backed and APCu-backed function memoization
25 * This class provides memoization for pure functions. A function is pure
26 * if its result value depends on nothing other than its input parameters
27 * and if invoking it does not cause any side-effects.
29 * The first invocation of the memoized callable with a particular set of
30 * arguments will be delegated to the underlying callable. Repeat invocations
31 * with the same input parameters will be served from APC or APCu.
35 * $memoizedStrrev = new MemoizedCallable( 'range' );
36 * $memoizedStrrev->invoke( 5, 8 ); // result: array( 5, 6, 7, 8 )
37 * $memoizedStrrev->invokeArgs( array( 5, 8 ) ); // same
38 * MemoizedCallable::call( 'range', array( 5, 8 ) ); // same
43 class MemoizedCallable
{
48 /** @var string Unique name of callable; used for cache keys. */
49 private $callableName;
55 * @throws InvalidArgumentException if $callable is not a callable.
56 * @param callable $callable Function or method to memoize.
57 * @param int $ttl TTL in seconds. Defaults to 3600 (1hr). Capped at 86400 (24h).
59 public function __construct( $callable, $ttl = 3600 ) {
60 if ( !is_callable( $callable, false, $this->callableName
) ) {
61 throw new InvalidArgumentException(
62 'Argument 1 passed to MemoizedCallable::__construct() must ' .
63 'be an instance of callable; ' . gettype( $callable ) . ' given'
67 if ( $this->callableName
=== 'Closure::__invoke' ) {
68 // Differentiate anonymous functions from one another
69 $this->callableName
.= uniqid();
72 $this->callable
= $callable;
73 $this->ttl
= min( max( $ttl, 1 ), 86400 );
77 * Fetch the result of a previous invocation from APC or APCu.
80 * @param bool &$success
83 protected function fetchResult( $key, &$success ) {
85 if ( function_exists( 'apc_fetch' ) ) {
86 return apc_fetch( $key, $success );
87 } elseif ( function_exists( 'apcu_fetch' ) ) {
88 return apcu_fetch( $key, $success );
94 * Store the result of an invocation in APC or APCu.
97 * @param mixed $result
99 protected function storeResult( $key, $result ) {
100 if ( function_exists( 'apc_store' ) ) {
101 apc_store( $key, $result, $this->ttl
);
102 } elseif ( function_exists( 'apcu_store' ) ) {
103 apcu_store( $key, $result, $this->ttl
);
108 * Invoke the memoized function or method.
110 * @throws InvalidArgumentException If parameters list contains non-scalar items.
111 * @param array $args Parameters for memoized function or method.
112 * @return mixed The memoized callable's return value.
114 public function invokeArgs( array $args = [] ) {
115 foreach ( $args as $arg ) {
116 if ( $arg !== null && !is_scalar( $arg ) ) {
117 throw new InvalidArgumentException(
118 'MemoizedCallable::invoke() called with non-scalar ' .
124 $hash = md5( serialize( $args ) );
125 $key = __CLASS__
. ':' . $this->callableName
. ':' . $hash;
127 $result = $this->fetchResult( $key, $success );
129 $result = ( $this->callable
)( ...$args );
130 $this->storeResult( $key, $result );
137 * Invoke the memoized function or method.
139 * Like MemoizedCallable::invokeArgs(), but variadic.
141 * @param mixed ...$params Parameters for memoized function or method.
142 * @return mixed The memoized callable's return value.
144 public function invoke( ...$params ) {
145 return $this->invokeArgs( $params );
149 * Shortcut method for creating a MemoizedCallable and invoking it
150 * with the specified arguments.
152 * @param callable $callable
157 public static function call( $callable, array $args = [], $ttl = 3600 ) {
158 $instance = new self( $callable, $ttl );
159 return $instance->invokeArgs( $args );