MediaWiki  master
CacheHelper.php
Go to the documentation of this file.
1 <?php
26 
45 class CacheHelper implements ICacheHelper {
52  protected $cacheExpiry = 3600;
53 
62  protected $cachedChunks;
63 
71  protected $hasCached = null;
72 
79  protected $cacheEnabled = true;
80 
87  protected $onInitHandler;
88 
95  protected $cacheKey = [];
96 
103  public function setCacheEnabled( $cacheEnabled ) {
104  $this->cacheEnabled = $cacheEnabled;
105  }
106 
116  public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
117  if ( is_null( $this->hasCached ) ) {
118  if ( !is_null( $cacheExpiry ) ) {
119  $this->cacheExpiry = $cacheExpiry;
120  }
121 
122  if ( !is_null( $cacheEnabled ) ) {
123  $this->setCacheEnabled( $cacheEnabled );
124  }
125 
126  $this->initCaching();
127  }
128  }
129 
141  public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) {
142  if ( $this->cacheExpiry < 86400 * 3650 ) {
143  $message = $context->msg(
144  'cachedspecial-viewing-cached-ttl',
145  $context->getLanguage()->formatDuration( $this->cacheExpiry )
146  )->escaped();
147  } else {
148  $message = $context->msg(
149  'cachedspecial-viewing-cached-ts'
150  )->escaped();
151  }
152 
153  if ( $includePurgeLink ) {
154  $refreshArgs = $context->getRequest()->getQueryValues();
155  unset( $refreshArgs['title'] );
156  $refreshArgs['action'] = 'purge';
157 
158  $message .= ' ' . MediaWikiServices::getInstance()->getLinkRenderer()->makeLink(
159  $context->getTitle(),
160  $context->msg( 'cachedspecial-refresh-now' )->text(),
161  [],
162  $refreshArgs
163  );
164  }
165 
166  return $message;
167  }
168 
175  protected function initCaching() {
176  if ( $this->cacheEnabled && is_null( $this->hasCached ) ) {
178 
179  $this->hasCached = is_array( $cachedChunks );
180  $this->cachedChunks = $this->hasCached ? $cachedChunks : [];
181 
182  if ( $this->onInitHandler !== null ) {
183  call_user_func( $this->onInitHandler, $this->hasCached );
184  }
185  }
186  }
187 
202  public function getCachedValue( $computeFunction, $args = [], $key = null ) {
203  $this->initCaching();
204 
205  if ( $this->cacheEnabled && $this->hasCached ) {
206  $value = null;
207 
208  if ( is_null( $key ) ) {
209  reset( $this->cachedChunks );
210  $itemKey = key( $this->cachedChunks );
211 
212  if ( !is_int( $itemKey ) ) {
213  wfWarn( "Attempted to get item with non-numeric key while " .
214  "the next item in the queue has a key ($itemKey) in " . __METHOD__ );
215  } elseif ( is_null( $itemKey ) ) {
216  wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
217  } else {
218  $value = array_shift( $this->cachedChunks );
219  }
220  } elseif ( array_key_exists( $key, $this->cachedChunks ) ) {
221  $value = $this->cachedChunks[$key];
222  unset( $this->cachedChunks[$key] );
223  } else {
224  wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
225  }
226  } else {
227  if ( !is_array( $args ) ) {
228  $args = [ $args ];
229  }
230 
231  $value = $computeFunction( ...$args );
232 
233  if ( $this->cacheEnabled ) {
234  if ( is_null( $key ) ) {
235  $this->cachedChunks[] = $value;
236  } else {
237  $this->cachedChunks[$key] = $value;
238  }
239  }
240  }
241 
242  return $value;
243  }
244 
251  public function saveCache() {
252  if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
253  wfGetCache( CACHE_ANYTHING )->set(
254  $this->getCacheKeyString(),
255  $this->cachedChunks,
256  $this->cacheExpiry
257  );
258  }
259  }
260 
269  public function setExpiry( $cacheExpiry ) {
270  $this->cacheExpiry = $cacheExpiry;
271  }
272 
282  protected function getCacheKeyString() {
283  if ( $this->cacheKey === [] ) {
284  throw new MWException( 'No cache key set, so cannot obtain or save the CacheHelper values.' );
285  }
286 
287  return ObjectCache::getLocalClusterInstance()->makeKey(
288  ...array_values( $this->cacheKey )
289  );
290  }
291 
299  public function setCacheKey( array $cacheKey ) {
300  $this->cacheKey = $cacheKey;
301  }
302 
310  public function rebuildOnDemand() {
311  $this->hasCached = false;
312  }
313 
321  public function setOnInitializedHandler( $handlerFunction ) {
322  $this->onInitHandler = $handlerFunction;
323  }
324 }
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
$context
Definition: load.php:45
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
wfGetCache( $cacheType)
Get a specific cache object.
int $cacheExpiry
The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry...
Definition: CacheHelper.php:52
static getLocalClusterInstance()
Get the main cluster-local cache object.
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:45
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry...
bool null $hasCached
Indicates if the to be cached content was already cached.
Definition: CacheHelper.php:71
getCachedNotice(IContextSource $context, $includePurgeLink=true)
Returns a message that notifies the user he/she is looking at a cached version of the page...
initCaching()
Initializes the caching if not already done so.
if( $line===false) $args
Definition: mcc.php:124
array $cachedChunks
List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
Definition: CacheHelper.php:62
msg( $key,... $params)
This is the method for getting translated interface messages.
callable null $onInitHandler
Function that gets called when initialization is done.
Definition: CacheHelper.php:87
setCacheKey(array $cacheKey)
Sets the cache key that should be used.
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
rebuildOnDemand()
Rebuild the content, even if it&#39;s already cached.
const CACHE_ANYTHING
Definition: Defines.php:81
bool $cacheEnabled
If the cache is enabled or not.
Definition: CacheHelper.php:79
setOnInitializedHandler( $handlerFunction)
Sets a function that gets called when initialization of the cache is done.
saveCache()
Saves the HTML to the cache in case it got recomputed.
getCacheKeyString()
Returns the cache key to use to cache this page&#39;s HTML output.
array $cacheKey
Elements to build a cache key with.
Definition: CacheHelper.php:95
Interface for all classes implementing CacheHelper functionality.
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.