MediaWiki
master
Toggle main menu visibility
MemoizedCallable.php
Go to the documentation of this file.
1
<?php
6
7
namespace
Wikimedia\ObjectCache;
8
9
use Closure;
10
use InvalidArgumentException;
11
35
class
MemoizedCallable
{
36
38
private
$callable;
39
41
private
$callableName;
42
44
private
$ttl;
45
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 a callable; '
. get_debug_type( $callable ) .
' given'
58
);
59
}
60
61
if
( $callable instanceof Closure ) {
62
throw
new
InvalidArgumentException(
'Cannot memoize unnamed closure'
);
63
}
64
65
if
( is_object( $callable ) || is_object( $callable[ 0 ] ) ) {
66
throw
new
InvalidArgumentException(
'Cannot memoize object-bound callable'
);
67
}
68
69
$this->callable = $callable;
70
$this->ttl = min( max( $ttl, 1 ), 86400 );
71
}
72
80
protected
function
fetchResult
( $key, &
$success
) {
81
$success
=
false
;
82
if
( function_exists(
'apcu_fetch'
) ) {
83
return
apcu_fetch( $key,
$success
);
84
}
85
return
false
;
86
}
87
94
protected
function
storeResult
( $key, $result ) {
95
if
( function_exists(
'apcu_store'
) ) {
96
apcu_store( $key, $result, $this->ttl );
97
}
98
}
99
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 = ( $this->callable )( ...$args );
123
$this->
storeResult
( $key, $result );
124
}
125
126
return
$result;
127
}
128
137
public
function
invoke
( ...$params ) {
138
return
$this->
invokeArgs
( $params );
139
}
140
150
public
static
function
call
( $callable, array $args = [], $ttl = 3600 ) {
151
$instance =
new
self
( $callable, $ttl );
152
return
$instance->invokeArgs( $args );
153
}
154
}
155
157
class_alias( MemoizedCallable::class,
'MemoizedCallable'
);
$success
$success
Definition
NoLocalSettings.php:24
Wikimedia\ObjectCache\MemoizedCallable
APCu-backed function memoization.
Definition
MemoizedCallable.php:35
Wikimedia\ObjectCache\MemoizedCallable\storeResult
storeResult( $key, $result)
Store the result of an invocation.
Definition
MemoizedCallable.php:94
Wikimedia\ObjectCache\MemoizedCallable\invoke
invoke(... $params)
Invoke the memoized function or method.
Definition
MemoizedCallable.php:137
Wikimedia\ObjectCache\MemoizedCallable\__construct
__construct( $callable, $ttl=3600)
Definition
MemoizedCallable.php:53
Wikimedia\ObjectCache\MemoizedCallable\invokeArgs
invokeArgs(array $args=[])
Invoke the memoized function or method.
Definition
MemoizedCallable.php:107
Wikimedia\ObjectCache\MemoizedCallable\fetchResult
fetchResult( $key, &$success)
Fetch the result of a previous invocation.
Definition
MemoizedCallable.php:80
Wikimedia\ObjectCache\MemoizedCallable\call
static call( $callable, array $args=[], $ttl=3600)
Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments.
Definition
MemoizedCallable.php:150
includes
libs
ObjectCache
MemoizedCallable.php
Generated on
for MediaWiki by
1.18.0