filebackend: use self:: instead of FileBackend:: for some constant uses
[lhc/web/wiklou.git] / includes / libs / MemoizedCallable.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @author Ori Livneh
20 */
21
22 /**
23 * APC-backed and APCu-backed function memoization
24 *
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.
28 *
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.
32 *
33 * @par Example:
34 * @code
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
39 * @endcode
40 *
41 * @since 1.27
42 */
43 class MemoizedCallable {
44
45 /** @var callable */
46 private $callable;
47
48 /** @var string Unique name of callable; used for cache keys. */
49 private $callableName;
50
51 /**
52 * @throws InvalidArgumentException if $callable is not a callable.
53 * @param callable $callable Function or method to memoize.
54 * @param int $ttl TTL in seconds. Defaults to 3600 (1hr). Capped at 86400 (24h).
55 */
56 public function __construct( $callable, $ttl = 3600 ) {
57 if ( !is_callable( $callable, false, $this->callableName ) ) {
58 throw new InvalidArgumentException(
59 'Argument 1 passed to MemoizedCallable::__construct() must ' .
60 'be an instance of callable; ' . gettype( $callable ) . ' given'
61 );
62 }
63
64 if ( $this->callableName === 'Closure::__invoke' ) {
65 // Differentiate anonymous functions from one another
66 $this->callableName .= uniqid();
67 }
68
69 $this->callable = $callable;
70 $this->ttl = min( max( $ttl, 1 ), 86400 );
71 }
72
73 /**
74 * Fetch the result of a previous invocation from APC or APCu.
75 *
76 * @param string $key
77 * @param bool &$success
78 * @return bool
79 */
80 protected function fetchResult( $key, &$success ) {
81 $success = false;
82 if ( function_exists( 'apc_fetch' ) ) {
83 return apc_fetch( $key, $success );
84 } elseif ( function_exists( 'apcu_fetch' ) ) {
85 return apcu_fetch( $key, $success );
86 }
87 return false;
88 }
89
90 /**
91 * Store the result of an invocation in APC or APCu.
92 *
93 * @param string $key
94 * @param mixed $result
95 */
96 protected function storeResult( $key, $result ) {
97 if ( function_exists( 'apc_store' ) ) {
98 apc_store( $key, $result, $this->ttl );
99 } elseif ( function_exists( 'apcu_store' ) ) {
100 apcu_store( $key, $result, $this->ttl );
101 }
102 }
103
104 /**
105 * Invoke the memoized function or method.
106 *
107 * @throws InvalidArgumentException If parameters list contains non-scalar items.
108 * @param array $args Parameters for memoized function or method.
109 * @return mixed The memoized callable's return value.
110 */
111 public function invokeArgs( array $args = [] ) {
112 foreach ( $args as $arg ) {
113 if ( $arg !== null && !is_scalar( $arg ) ) {
114 throw new InvalidArgumentException(
115 'MemoizedCallable::invoke() called with non-scalar ' .
116 'argument'
117 );
118 }
119 }
120
121 $hash = md5( serialize( $args ) );
122 $key = __CLASS__ . ':' . $this->callableName . ':' . $hash;
123 $success = false;
124 $result = $this->fetchResult( $key, $success );
125 if ( !$success ) {
126 $result = ( $this->callable )( ...$args );
127 $this->storeResult( $key, $result );
128 }
129
130 return $result;
131 }
132
133 /**
134 * Invoke the memoized function or method.
135 *
136 * Like MemoizedCallable::invokeArgs(), but variadic.
137 *
138 * @param mixed ...$params Parameters for memoized function or method.
139 * @return mixed The memoized callable's return value.
140 */
141 public function invoke( ...$params ) {
142 return $this->invokeArgs( $params );
143 }
144
145 /**
146 * Shortcut method for creating a MemoizedCallable and invoking it
147 * with the specified arguments.
148 *
149 * @param callable $callable
150 * @param array $args
151 * @param int $ttl
152 * @return mixed
153 */
154 public static function call( $callable, array $args = [], $ttl = 3600 ) {
155 $instance = new self( $callable, $ttl );
156 return $instance->invokeArgs( $args );
157 }
158 }