MediaWiki  1.27.2
CacheDependency.php
Go to the documentation of this file.
1 <?php
31  private $value;
33  private $deps;
34 
41  function __construct( $value = false, $deps = [] ) {
42  $this->value = $value;
43 
44  if ( !is_array( $deps ) ) {
45  $deps = [ $deps ];
46  }
47 
48  $this->deps = $deps;
49  }
50 
56  function isExpired() {
57  foreach ( $this->deps as $dep ) {
58  if ( $dep->isExpired() ) {
59  return true;
60  }
61  }
62 
63  return false;
64  }
65 
70  function initialiseDeps() {
71  foreach ( $this->deps as $dep ) {
72  $dep->loadDependencyValues();
73  }
74  }
75 
80  function getValue() {
81  return $this->value;
82  }
83 
91  function storeToCache( $cache, $key, $expiry = 0 ) {
92  $this->initialiseDeps();
93  $cache->set( $key, $this, $expiry );
94  }
95 
113  static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
114  $callbackParams = [], $deps = []
115  ) {
116  $obj = $cache->get( $key );
117 
118  if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
119  $value = $obj->value;
120  } elseif ( $callback ) {
121  $value = call_user_func_array( $callback, $callbackParams );
122  # Cache the newly-generated value
123  $wrapper = new DependencyWrapper( $value, $deps );
124  $wrapper->storeToCache( $cache, $key, $expiry );
125  } else {
126  $value = null;
127  }
128 
129  return $value;
130  }
131 }
132 
136 abstract class CacheDependency {
140  abstract function isExpired();
141 
145  function loadDependencyValues() {
146  }
147 }
148 
153  private $filename;
154  private $timestamp;
155 
168  function __construct( $filename, $timestamp = null ) {
169  $this->filename = $filename;
170  $this->timestamp = $timestamp;
171  }
172 
176  function __sleep() {
177  $this->loadDependencyValues();
178 
179  return [ 'filename', 'timestamp' ];
180  }
181 
182  function loadDependencyValues() {
183  if ( is_null( $this->timestamp ) ) {
184  MediaWiki\suppressWarnings();
185  # Dependency on a non-existent file stores "false"
186  # This is a valid concept!
187  $this->timestamp = filemtime( $this->filename );
188  MediaWiki\restoreWarnings();
189  }
190  }
191 
195  function isExpired() {
196  MediaWiki\suppressWarnings();
197  $lastmod = filemtime( $this->filename );
198  MediaWiki\restoreWarnings();
199  if ( $lastmod === false ) {
200  if ( $this->timestamp === false ) {
201  # Still nonexistent
202  return false;
203  } else {
204  # Deleted
205  wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
206 
207  return true;
208  }
209  } else {
210  if ( $lastmod > $this->timestamp ) {
211  # Modified or created
212  wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
213 
214  return true;
215  } else {
216  # Not modified
217  return false;
218  }
219  }
220  }
221 }
222 
227  private $name;
228  private $value;
229 
230  function __construct( $name ) {
231  $this->name = $name;
232  $this->value = $GLOBALS[$name];
233  }
234 
238  function isExpired() {
239  if ( !isset( $GLOBALS[$this->name] ) ) {
240  return true;
241  }
242 
243  return $GLOBALS[$this->name] != $this->value;
244  }
245 }
246 
251  private $name;
252  private $value;
253 
254  function __construct( $name ) {
255  $this->name = $name;
256  $this->value = constant( $name );
257  }
258 
262  function isExpired() {
263  return constant( $this->name ) != $this->value;
264  }
265 }
storeToCache($cache, $key, $expiry=0)
Store the wrapper to a cache.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
static getValueFromCache($cache, $key, $expiry=0, $callback=false, $callbackParams=[], $deps=[])
Attempt to get a value from the cache.
This class stores an arbitrary value along with its dependencies.
getValue()
Get the user-defined value.
__construct($filename, $timestamp=null)
Create a file dependency.
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.