MediaWiki  1.34.4
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 }
DependencyWrapper\getValue
getValue()
Get the user-defined value.
Definition: DependencyWrapper.php:79
DependencyWrapper\isExpired
isExpired()
Returns true if any of the dependencies have expired.
Definition: DependencyWrapper.php:55
DependencyWrapper
This class stores an arbitrary value along with its dependencies.
Definition: DependencyWrapper.php:30
DependencyWrapper\storeToCache
storeToCache( $cache, $key, $expiry=0)
Store the wrapper to a cache.
Definition: DependencyWrapper.php:90
DependencyWrapper\__construct
__construct( $value=false, $deps=[])
Definition: DependencyWrapper.php:40
DependencyWrapper\$value
$value
Definition: DependencyWrapper.php:31
CacheDependency
Definition: CacheDependency.php:27
$cache
$cache
Definition: mcc.php:33
DependencyWrapper\$deps
CacheDependency[] $deps
Definition: DependencyWrapper.php:33
DependencyWrapper\getValueFromCache
static getValueFromCache( $cache, $key, $expiry=0, $callback=false, $callbackParams=[], $deps=[])
Attempt to get a value from the cache.
Definition: DependencyWrapper.php:112
DependencyWrapper\initialiseDeps
initialiseDeps()
Initialise dependency values in preparation for storing.
Definition: DependencyWrapper.php:69