Merge "Convert article delete to use OOUI"
[lhc/web/wiklou.git] / includes / libs / MemoizedCallable.php
1 <?php
2 /**
3 * APC-backed and APCu-backed function memoization
4 *
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.
8 *
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.
12 *
13 * @par Example:
14 * @code
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
19 * @endcode
20 *
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.
25 *
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.
30 *
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
35 *
36 * @file
37 * @author Ori Livneh
38 * @since 1.27
39 */
40 class MemoizedCallable {
41
42 /** @var callable */
43 private $callable;
44
45 /** @var string Unique name of callable; used for cache keys. */
46 private $callableName;
47
48 /**
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).
52 */
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'
58 );
59 }
60
61 if ( $this->callableName === 'Closure::__invoke' ) {
62 // Differentiate anonymous functions from one another
63 $this->callableName .= uniqid();
64 }
65
66 $this->callable = $callable;
67 $this->ttl = min( max( $ttl, 1 ), 86400 );
68 }
69
70 /**
71 * Fetch the result of a previous invocation from APC or APCu.
72 *
73 * @param string $key
74 * @param bool &$success
75 */
76 protected function fetchResult( $key, &$success ) {
77 $success = false;
78 if ( function_exists( 'apc_fetch' ) ) {
79 return apc_fetch( $key, $success );
80 } elseif ( function_exists( 'apcu_fetch' ) ) {
81 return apcu_fetch( $key, $success );
82 }
83 return false;
84 }
85
86 /**
87 * Store the result of an invocation in APC or APCu.
88 *
89 * @param string $key
90 * @param mixed $result
91 */
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 );
97 }
98 }
99
100 /**
101 * Invoke the memoized function or method.
102 *
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.
106 */
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 ' .
112 'argument'
113 );
114 }
115 }
116
117 $hash = md5( serialize( $args ) );
118 $key = __CLASS__ . ':' . $this->callableName . ':' . $hash;
119 $success = false;
120 $result = $this->fetchResult( $key, $success );
121 if ( !$success ) {
122 $result = call_user_func_array( $this->callable, $args );
123 $this->storeResult( $key, $result );
124 }
125
126 return $result;
127 }
128
129 /**
130 * Invoke the memoized function or method.
131 *
132 * Like MemoizedCallable::invokeArgs(), but variadic.
133 *
134 * @param mixed ...$params Parameters for memoized function or method.
135 * @return mixed The memoized callable's return value.
136 */
137 public function invoke() {
138 return $this->invokeArgs( func_get_args() );
139 }
140
141 /**
142 * Shortcut method for creating a MemoizedCallable and invoking it
143 * with the specified arguments.
144 *
145 * @param callable $callable
146 * @param array $args
147 * @param int $ttl
148 */
149 public static function call( $callable, array $args = [], $ttl = 3600 ) {
150 $instance = new self( $callable, $ttl );
151 return $instance->invokeArgs( $args );
152 }
153 }