MediaWiki REL1_34
DependencyWrapper.php
Go to the documentation of this file.
1<?php
31 private $value;
33 private $deps;
34
40 function __construct( $value = false, $deps = [] ) {
41 $this->value = $value;
42
43 if ( !is_array( $deps ) ) {
44 $deps = [ $deps ];
45 }
46
47 $this->deps = $deps;
48 }
49
55 function isExpired() {
56 foreach ( $this->deps as $dep ) {
57 if ( $dep->isExpired() ) {
58 return true;
59 }
60 }
61
62 return false;
63 }
64
69 function initialiseDeps() {
70 foreach ( $this->deps as $dep ) {
71 $dep->loadDependencyValues();
72 }
73 }
74
79 function getValue() {
80 return $this->value;
81 }
82
90 function storeToCache( $cache, $key, $expiry = 0 ) {
91 $this->initialiseDeps();
92 $cache->set( $key, $this, $expiry );
93 }
94
112 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
113 $callbackParams = [], $deps = []
114 ) {
115 $obj = $cache->get( $key );
116
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 );
124 } else {
125 $value = null;
126 }
127
128 return $value;
129 }
130}
This class stores an arbitrary value along with its dependencies.
__construct( $value=false, $deps=[])
initialiseDeps()
Initialise dependency values in preparation for storing.
static getValueFromCache( $cache, $key, $expiry=0, $callback=false, $callbackParams=[], $deps=[])
Attempt to get a value from the cache.
CacheDependency[] $deps
storeToCache( $cache, $key, $expiry=0)
Store the wrapper to a cache.
getValue()
Get the user-defined value.
isExpired()
Returns true if any of the dependencies have expired.
$cache
Definition mcc.php:33