MediaWiki  1.28.0
CacheDependency.php
Go to the documentation of this file.
1 <?php
24 
32  private $value;
34  private $deps;
35 
42  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 
57  function isExpired() {
58  foreach ( $this->deps as $dep ) {
59  if ( $dep->isExpired() ) {
60  return true;
61  }
62  }
63 
64  return false;
65  }
66 
71  function initialiseDeps() {
72  foreach ( $this->deps as $dep ) {
73  $dep->loadDependencyValues();
74  }
75  }
76 
81  function getValue() {
82  return $this->value;
83  }
84 
92  function storeToCache( $cache, $key, $expiry = 0 ) {
93  $this->initialiseDeps();
94  $cache->set( $key, $this, $expiry );
95  }
96 
114  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 = call_user_func_array( $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 }
133 
137 abstract class CacheDependency {
141  abstract function isExpired();
142 
146  function loadDependencyValues() {
147  }
148 }
149 
154  private $filename;
155  private $timestamp;
156 
169  function __construct( $filename, $timestamp = null ) {
170  $this->filename = $filename;
171  $this->timestamp = $timestamp;
172  }
173 
177  function __sleep() {
178  $this->loadDependencyValues();
179 
180  return [ 'filename', 'timestamp' ];
181  }
182 
183  function loadDependencyValues() {
184  if ( is_null( $this->timestamp ) ) {
185  MediaWiki\suppressWarnings();
186  # Dependency on a non-existent file stores "false"
187  # This is a valid concept!
188  $this->timestamp = filemtime( $this->filename );
189  MediaWiki\restoreWarnings();
190  }
191  }
192 
196  function isExpired() {
197  MediaWiki\suppressWarnings();
198  $lastmod = filemtime( $this->filename );
199  MediaWiki\restoreWarnings();
200  if ( $lastmod === false ) {
201  if ( $this->timestamp === false ) {
202  # Still nonexistent
203  return false;
204  } else {
205  # Deleted
206  wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
207 
208  return true;
209  }
210  } else {
211  if ( $lastmod > $this->timestamp ) {
212  # Modified or created
213  wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
214 
215  return true;
216  } else {
217  # Not modified
218  return false;
219  }
220  }
221  }
222 }
223 
228  private $name;
229  private $value;
230 
231  function __construct( $name ) {
232  $this->name = $name;
233  $this->value = $GLOBALS[$name];
234  }
235 
239  function isExpired() {
240  if ( !isset( $GLOBALS[$this->name] ) ) {
241  return true;
242  }
243 
244  return $GLOBALS[$this->name] != $this->value;
245  }
246 }
247 
252  private $name;
253  private $value;
254 
255  function __construct( $name ) {
256  $this->name = $name;
257  $this->value = $this->getConfig()->get( $this->name );
258  }
259 
260  private function getConfig() {
261  return MediaWikiServices::getInstance()->getMainConfig();
262  }
263 
267  function isExpired() {
268  if ( !$this->getConfig()->has( $this->name ) ) {
269  return true;
270  }
271 
272  return $this->getConfig()->get( $this->name ) != $this->value;
273  }
274 }
275 
280  private $name;
281  private $value;
282 
283  function __construct( $name ) {
284  $this->name = $name;
285  $this->value = constant( $name );
286  }
287 
291  function isExpired() {
292  return constant( $this->name ) != $this->value;
293  }
294 }
storeToCache($cache, $key, $expiry=0)
Store the wrapper to a cache.
static getValueFromCache($cache, $key, $expiry=0, $callback=false, $callbackParams=[], $deps=[])
Attempt to get a value from the cache.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
This class stores an arbitrary value along with its dependencies.
getValue()
Get the user-defined value.
__construct($filename, $timestamp=null)
Create a file dependency.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
isExpired()
Returns true if the dependency is expired, false otherwise.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
$GLOBALS['IP']
$cache
Definition: mcc.php:33
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
__construct($value=false, $deps=[])
Create an instance.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at name
Definition: design.txt:12
CacheDependency[] $deps
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
initialiseDeps()
Initialise dependency values in preparation for storing.
isExpired()
Returns true if any of the dependencies have expired.