3 * Data caching with dependencies.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * This class stores an arbitrary value along with its dependencies.
26 * Users should typically only use DependencyWrapper::getValueFromCache(),
27 * rather than instantiating one of these objects directly.
30 class DependencyWrapper
{
32 /** @var CacheDependency[] */
36 * @param mixed $value The user-supplied value
37 * @param CacheDependency|CacheDependency[] $deps A dependency or dependency
38 * array. All dependencies must be objects implementing CacheDependency.
40 function __construct( $value = false, $deps = [] ) {
41 $this->value
= $value;
43 if ( !is_array( $deps ) ) {
51 * Returns true if any of the dependencies have expired
55 function isExpired() {
56 foreach ( $this->deps
as $dep ) {
57 if ( $dep->isExpired() ) {
66 * Initialise dependency values in preparation for storing. This must be
67 * called before serialization.
69 function initialiseDeps() {
70 foreach ( $this->deps
as $dep ) {
71 $dep->loadDependencyValues();
76 * Get the user-defined value
84 * Store the wrapper to a cache
86 * @param BagOStuff $cache
90 function storeToCache( $cache, $key, $expiry = 0 ) {
91 $this->initialiseDeps();
92 $cache->set( $key, $this, $expiry );
96 * Attempt to get a value from the cache. If the value is expired or missing,
97 * it will be generated with the callback function (if present), and the newly
98 * calculated value will be stored to the cache in a wrapper.
100 * @param BagOStuff $cache
101 * @param string $key The cache key
102 * @param int $expiry The expiry timestamp or interval in seconds
103 * @param bool|callable $callback The callback for generating the value, or false
104 * @param array $callbackParams The function parameters for the callback
105 * @param array $deps The dependencies to store on a cache miss. Note: these
106 * are not the dependencies used on a cache hit! Cache hits use the stored
109 * @return mixed The value, or null if it was not present in the cache and no
110 * callback was defined.
112 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
113 $callbackParams = [], $deps = []
115 $obj = $cache->get( $key );
117 if ( is_object( $obj ) && $obj instanceof DependencyWrapper
&& !$obj->isExpired() ) {
118 $value = $obj->value
;
119 } elseif ( $callback ) {
120 $value = $callback( ...$callbackParams );
121 # Cache the newly-generated value
122 $wrapper = new DependencyWrapper( $value, $deps );
123 $wrapper->storeToCache( $cache, $key, $expiry );