2015-10-05 23:10:56 +00:00
|
|
|
<?php
|
2017-07-18 01:24:32 +00:00
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Ori Livneh
|
|
|
|
|
*/
|
|
|
|
|
|
2015-10-05 23:10:56 +00:00
|
|
|
/**
|
2016-10-01 17:00:53 +00:00
|
|
|
* APC-backed and APCu-backed function memoization
|
2015-10-05 23:10:56 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
2016-10-01 17:00:53 +00:00
|
|
|
* with the same input parameters will be served from APC or APCu.
|
2015-10-05 23:10:56 +00:00
|
|
|
*
|
|
|
|
|
* @par Example:
|
|
|
|
|
* @code
|
|
|
|
|
* $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
|
|
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
class MemoizedCallable {
|
|
|
|
|
|
|
|
|
|
/** @var callable */
|
|
|
|
|
private $callable;
|
|
|
|
|
|
|
|
|
|
/** @var string Unique name of callable; used for cache keys. */
|
|
|
|
|
private $callableName;
|
|
|
|
|
|
2019-09-11 08:57:15 +00:00
|
|
|
/** @var int */
|
|
|
|
|
private $ttl;
|
|
|
|
|
|
2015-10-05 23:10:56 +00:00
|
|
|
/**
|
|
|
|
|
* @throws InvalidArgumentException if $callable is not a callable.
|
|
|
|
|
* @param callable $callable Function or method to memoize.
|
|
|
|
|
* @param int $ttl TTL in seconds. Defaults to 3600 (1hr). Capped at 86400 (24h).
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $callable, $ttl = 3600 ) {
|
|
|
|
|
if ( !is_callable( $callable, false, $this->callableName ) ) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
'Argument 1 passed to MemoizedCallable::__construct() must ' .
|
|
|
|
|
'be an instance of callable; ' . gettype( $callable ) . ' given'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->callableName === 'Closure::__invoke' ) {
|
|
|
|
|
// Differentiate anonymous functions from one another
|
|
|
|
|
$this->callableName .= uniqid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->callable = $callable;
|
|
|
|
|
$this->ttl = min( max( $ttl, 1 ), 86400 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-01 17:00:53 +00:00
|
|
|
* Fetch the result of a previous invocation from APC or APCu.
|
2015-10-05 23:10:56 +00:00
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param bool &$success
|
2017-09-09 20:47:04 +00:00
|
|
|
* @return bool
|
2015-10-05 23:10:56 +00:00
|
|
|
*/
|
|
|
|
|
protected function fetchResult( $key, &$success ) {
|
|
|
|
|
$success = false;
|
|
|
|
|
if ( function_exists( 'apc_fetch' ) ) {
|
|
|
|
|
return apc_fetch( $key, $success );
|
2016-10-01 17:00:53 +00:00
|
|
|
} elseif ( function_exists( 'apcu_fetch' ) ) {
|
|
|
|
|
return apcu_fetch( $key, $success );
|
2015-10-05 23:10:56 +00:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-01 17:00:53 +00:00
|
|
|
* Store the result of an invocation in APC or APCu.
|
2015-10-05 23:10:56 +00:00
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $result
|
|
|
|
|
*/
|
|
|
|
|
protected function storeResult( $key, $result ) {
|
|
|
|
|
if ( function_exists( 'apc_store' ) ) {
|
|
|
|
|
apc_store( $key, $result, $this->ttl );
|
2016-10-01 17:00:53 +00:00
|
|
|
} elseif ( function_exists( 'apcu_store' ) ) {
|
|
|
|
|
apcu_store( $key, $result, $this->ttl );
|
2015-10-05 23:10:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invoke the memoized function or method.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException If parameters list contains non-scalar items.
|
|
|
|
|
* @param array $args Parameters for memoized function or method.
|
|
|
|
|
* @return mixed The memoized callable's return value.
|
|
|
|
|
*/
|
2016-02-17 10:31:52 +00:00
|
|
|
public function invokeArgs( array $args = [] ) {
|
2015-10-05 23:10:56 +00:00
|
|
|
foreach ( $args as $arg ) {
|
|
|
|
|
if ( $arg !== null && !is_scalar( $arg ) ) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
'MemoizedCallable::invoke() called with non-scalar ' .
|
|
|
|
|
'argument'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hash = md5( serialize( $args ) );
|
|
|
|
|
$key = __CLASS__ . ':' . $this->callableName . ':' . $hash;
|
|
|
|
|
$success = false;
|
|
|
|
|
$result = $this->fetchResult( $key, $success );
|
|
|
|
|
if ( !$success ) {
|
2018-06-09 23:26:32 +00:00
|
|
|
$result = ( $this->callable )( ...$args );
|
2015-10-05 23:10:56 +00:00
|
|
|
$this->storeResult( $key, $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invoke the memoized function or method.
|
|
|
|
|
*
|
|
|
|
|
* Like MemoizedCallable::invokeArgs(), but variadic.
|
|
|
|
|
*
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
* @param mixed ...$params Parameters for memoized function or method.
|
2015-10-05 23:10:56 +00:00
|
|
|
* @return mixed The memoized callable's return value.
|
|
|
|
|
*/
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
public function invoke( ...$params ) {
|
|
|
|
|
return $this->invokeArgs( $params );
|
2015-10-05 23:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shortcut method for creating a MemoizedCallable and invoking it
|
|
|
|
|
* with the specified arguments.
|
|
|
|
|
*
|
|
|
|
|
* @param callable $callable
|
|
|
|
|
* @param array $args
|
|
|
|
|
* @param int $ttl
|
2017-09-09 20:47:04 +00:00
|
|
|
* @return mixed
|
2015-10-05 23:10:56 +00:00
|
|
|
*/
|
2016-02-17 10:31:52 +00:00
|
|
|
public static function call( $callable, array $args = [], $ttl = 3600 ) {
|
2015-10-05 23:10:56 +00:00
|
|
|
$instance = new self( $callable, $ttl );
|
|
|
|
|
return $instance->invokeArgs( $args );
|
|
|
|
|
}
|
|
|
|
|
}
|