<?php
/**
- * APC-backed function memoization
+ * 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
*
* 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.
+ * with the same input parameters will be served from APC or APCu.
*
* @par Example:
* @code
}
/**
- * Fetch the result of a previous invocation from APC.
+ * Fetch the result of a previous invocation from APC or APCu.
*
* @param string $key
* @param bool &$success
$success = false;
if ( function_exists( 'apc_fetch' ) ) {
return apc_fetch( $key, $success );
+ } elseif ( function_exists( 'apcu_fetch' ) ) {
+ return apcu_fetch( $key, $success );
}
return false;
}
/**
- * Store the result of an invocation in APC.
+ * Store the result of an invocation in APC or APCu.
*
* @param string $key
* @param mixed $result
protected function storeResult( $key, $result ) {
if ( function_exists( 'apc_store' ) ) {
apc_store( $key, $result, $this->ttl );
+ } elseif ( function_exists( 'apcu_store' ) ) {
+ apcu_store( $key, $result, $this->ttl );
}
}
* @param array $args Parameters for memoized function or method.
* @return mixed The memoized callable's return value.
*/
- public function invokeArgs( Array $args = array() ) {
+ public function invokeArgs( array $args = [] ) {
foreach ( $args as $arg ) {
if ( $arg !== null && !is_scalar( $arg ) ) {
throw new InvalidArgumentException(
* @param array $args
* @param int $ttl
*/
- public static function call( $callable, Array $args = array(), $ttl = 3600 ) {
+ public static function call( $callable, array $args = [], $ttl = 3600 ) {
$instance = new self( $callable, $ttl );
return $instance->invokeArgs( $args );
}