MediaWiki REL1_39
DependencyWrapper.php
Go to the documentation of this file.
1<?php
30 private $value;
32 private $deps;
33
39 public function __construct( $value = false, $deps = [] ) {
40 $this->value = $value;
41
42 if ( !is_array( $deps ) ) {
43 $deps = [ $deps ];
44 }
45
46 $this->deps = $deps;
47 }
48
54 public function isExpired() {
55 foreach ( $this->deps as $dep ) {
56 if ( $dep->isExpired() ) {
57 return true;
58 }
59 }
60
61 return false;
62 }
63
68 public function initialiseDeps() {
69 foreach ( $this->deps as $dep ) {
70 $dep->loadDependencyValues();
71 }
72 }
73
78 public function getValue() {
79 return $this->value;
80 }
81
89 public function storeToCache( $cache, $key, $expiry = 0 ) {
90 $this->initialiseDeps();
91 $cache->set( $key, $this, $expiry );
92 }
93
111 public static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
112 $callbackParams = [], $deps = []
113 ) {
114 $obj = $cache->get( $key );
115
116 if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
117 $value = $obj->value;
118 } elseif ( $callback ) {
119 $value = $callback( ...$callbackParams );
120 # Cache the newly-generated value
121 $wrapper = new DependencyWrapper( $value, $deps );
122 $wrapper->storeToCache( $cache, $key, $expiry );
123 } else {
124 $value = null;
125 }
126
127 return $value;
128 }
129}
Base class to represent dependencies for LocalisationCache entries.
Store an arbitrary value whilst representing several CacheDependency objects as one.
__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.
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