Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
DependencyWrapper | |
0.00% |
0 / 24 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
isExpired | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
initialiseDeps | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
storeToCache | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getValueFromCache | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 |
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 | */ |
20 | |
21 | use Wikimedia\ObjectCache\BagOStuff; |
22 | |
23 | /** |
24 | * Store an arbitrary value whilst representing several CacheDependency objects as one. |
25 | * |
26 | * You should typically only use DependencyWrapper::getValueFromCache(), |
27 | * rather than instantiating one of these objects directly. |
28 | * |
29 | * @ingroup Language |
30 | */ |
31 | class DependencyWrapper { |
32 | /** @var mixed */ |
33 | private $value; |
34 | /** @var CacheDependency[] */ |
35 | private $deps; |
36 | |
37 | /** |
38 | * @param mixed $value The user-supplied value |
39 | * @param CacheDependency|CacheDependency[] $deps A dependency or dependency |
40 | * array. All dependencies must be objects implementing CacheDependency. |
41 | */ |
42 | public function __construct( $value = false, $deps = [] ) { |
43 | $this->value = $value; |
44 | |
45 | if ( !is_array( $deps ) ) { |
46 | $deps = [ $deps ]; |
47 | } |
48 | |
49 | $this->deps = $deps; |
50 | } |
51 | |
52 | /** |
53 | * Returns true if any of the dependencies have expired |
54 | * |
55 | * @return bool |
56 | */ |
57 | public function isExpired() { |
58 | foreach ( $this->deps as $dep ) { |
59 | if ( $dep->isExpired() ) { |
60 | return true; |
61 | } |
62 | } |
63 | |
64 | return false; |
65 | } |
66 | |
67 | /** |
68 | * Initialise dependency values in preparation for storing. This must be |
69 | * called before serialization. |
70 | */ |
71 | public function initialiseDeps() { |
72 | foreach ( $this->deps as $dep ) { |
73 | $dep->loadDependencyValues(); |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * Get the user-defined value |
79 | * @return bool|mixed |
80 | */ |
81 | public function getValue() { |
82 | return $this->value; |
83 | } |
84 | |
85 | /** |
86 | * Store the wrapper to a cache |
87 | * |
88 | * @param BagOStuff $cache |
89 | * @param string $key |
90 | * @param int $expiry |
91 | */ |
92 | public function storeToCache( $cache, $key, $expiry = 0 ) { |
93 | $this->initialiseDeps(); |
94 | $cache->set( $key, $this, $expiry ); |
95 | } |
96 | |
97 | /** |
98 | * Attempt to get a value from the cache. If the value is expired or missing, |
99 | * it will be generated with the callback function (if present), and the newly |
100 | * calculated value will be stored to the cache in a wrapper. |
101 | * |
102 | * @param BagOStuff $cache |
103 | * @param string $key The cache key |
104 | * @param int $expiry The expiry timestamp or interval in seconds |
105 | * @param callable|false $callback The callback for generating the value, or false |
106 | * @param array $callbackParams The function parameters for the callback |
107 | * @param array $deps The dependencies to store on a cache miss. Note: these |
108 | * are not the dependencies used on a cache hit! Cache hits use the stored |
109 | * dependency array. |
110 | * |
111 | * @return mixed The value, or null if it was not present in the cache and no |
112 | * callback was defined. |
113 | */ |
114 | public static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false, |
115 | $callbackParams = [], $deps = [] |
116 | ) { |
117 | $obj = $cache->get( $key ); |
118 | |
119 | if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) { |
120 | $value = $obj->value; |
121 | } elseif ( $callback ) { |
122 | $value = $callback( ...$callbackParams ); |
123 | # Cache the newly-generated value |
124 | $wrapper = new DependencyWrapper( $value, $deps ); |
125 | $wrapper->storeToCache( $cache, $key, $expiry ); |
126 | } else { |
127 | $value = null; |
128 | } |
129 | |
130 | return $value; |
131 | } |
132 | } |