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 
97  public function __construct() {
98  wfDeprecated( __CLASS__, '1.36' );
99  }
100 
107  public function setCacheEnabled( $cacheEnabled ) {
108  $this->cacheEnabled = $cacheEnabled;
109  }
110 
120  public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
121  if ( $this->hasCached === null ) {
122  if ( $cacheExpiry !== null ) {
123  $this->cacheExpiry = $cacheExpiry;
124  }
125 
126  if ( $cacheEnabled !== null ) {
127  $this->setCacheEnabled( $cacheEnabled );
128  }
129 
130  $this->initCaching();
131  }
132  }
133 
145  public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) {
146  if ( $this->cacheExpiry < 86400 * 3650 ) {
147  $message = $context->msg(
148  'cachedspecial-viewing-cached-ttl',
149  $context->getLanguage()->formatDuration( $this->cacheExpiry )
150  )->escaped();
151  } else {
152  $message = $context->msg(
153  'cachedspecial-viewing-cached-ts'
154  )->escaped();
155  }
156 
157  if ( $includePurgeLink ) {
158  $refreshArgs = $context->getRequest()->getQueryValues();
159  unset( $refreshArgs['title'] );
160  $refreshArgs['action'] = 'purge';
161 
162  $message .= ' ' . MediaWikiServices::getInstance()->getLinkRenderer()->makeLink(
163  $context->getTitle(),
164  $context->msg( 'cachedspecial-refresh-now' )->text(),
165  [],
166  $refreshArgs
167  );
168  }
169 
170  return $message;
171  }
172 
179  protected function initCaching() {
180  if ( $this->cacheEnabled && $this->hasCached === null ) {
182 
183  $this->hasCached = is_array( $cachedChunks );
184  $this->cachedChunks = $this->hasCached ? $cachedChunks : [];
185 
186  if ( $this->onInitHandler !== null ) {
187  call_user_func( $this->onInitHandler, $this->hasCached );
188  }
189  }
190  }
191 
206  public function getCachedValue( $computeFunction, $args = [], $key = null ) {
207  $this->initCaching();
208 
209  if ( $this->cacheEnabled && $this->hasCached ) {
210  $value = null;
211 
212  if ( $key === null ) {
213  reset( $this->cachedChunks );
214  $itemKey = key( $this->cachedChunks );
215 
216  if ( $itemKey === null ) {
217  wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
218  } elseif ( !is_int( $itemKey ) ) {
219  wfWarn( "Attempted to get item with non-numeric key while " .
220  "the next item in the queue has a key ($itemKey) in " . __METHOD__ );
221  } else {
222  $value = array_shift( $this->cachedChunks );
223  }
224  } elseif ( array_key_exists( $key, $this->cachedChunks ) ) {
225  $value = $this->cachedChunks[$key];
226  unset( $this->cachedChunks[$key] );
227  } else {
228  wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
229  }
230  } else {
231  if ( !is_array( $args ) ) {
232  $args = [ $args ];
233  }
234 
235  $value = $computeFunction( ...$args );
236 
237  if ( $this->cacheEnabled ) {
238  if ( $key === null ) {
239  $this->cachedChunks[] = $value;
240  } else {
241  $this->cachedChunks[$key] = $value;
242  }
243  }
244  }
245 
246  return $value;
247  }
248 
255  public function saveCache() {
256  if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
258  $this->getCacheKeyString(),
259  $this->cachedChunks,
260  $this->cacheExpiry
261  );
262  }
263  }
264 
273  public function setExpiry( $cacheExpiry ) {
274  $this->cacheExpiry = $cacheExpiry;
275  }
276 
286  protected function getCacheKeyString() {
287  if ( $this->cacheKey === [] ) {
288  throw new MWException( 'No cache key set, so cannot obtain or save the CacheHelper values.' );
289  }
290 
291  return ObjectCache::getLocalClusterInstance()->makeKey(
292  ...array_values( $this->cacheKey )
293  );
294  }
295 
303  public function setCacheKey( array $cacheKey ) {
304  $this->cacheKey = $cacheKey;
305  }
306 
314  public function rebuildOnDemand() {
315  $this->hasCached = false;
316  }
317 
325  public function setOnInitializedHandler( $handlerFunction ) {
326  $this->onInitHandler = $handlerFunction;
327  }
328 }
CacheHelper\startCache
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
Definition: CacheHelper.php:120
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:85
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:272
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:173
CacheHelper\getCachedNotice
getCachedNotice(IContextSource $context, $includePurgeLink=true)
Returns a message that notifies the user he/she is looking at a cached version of the page,...
Definition: CacheHelper.php:145
CacheHelper\setExpiry
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry....
Definition: CacheHelper.php:273
ICacheHelper
Interface for all classes implementing CacheHelper functionality.
Definition: ICacheHelper.php:31
CacheHelper\setCacheEnabled
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
Definition: CacheHelper.php:107
CacheHelper\$cachedChunks
array $cachedChunks
List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
Definition: CacheHelper.php:62
CacheHelper\$cacheExpiry
int $cacheExpiry
The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
Definition: CacheHelper.php:52
MWException
MediaWiki exception.
Definition: MWException.php:29
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1034
MessageLocalizer\msg
msg( $key,... $params)
This is the method for getting translated interface messages.
CacheHelper\$hasCached
bool null $hasCached
Indicates if the to be cached content was already cached.
Definition: CacheHelper.php:71
ObjectCache\getInstance
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:74
$args
if( $line===false) $args
Definition: mcc.php:124
CacheHelper\initCaching
initCaching()
Initializes the caching if not already done so.
Definition: CacheHelper.php:179
CacheHelper\setCacheKey
setCacheKey(array $cacheKey)
Sets the cache key that should be used.
Definition: CacheHelper.php:303
CacheHelper\getCachedValue
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
Definition: CacheHelper.php:206
CacheHelper\$onInitHandler
callable null $onInitHandler
Function that gets called when initialization is done.
Definition: CacheHelper.php:87
CacheHelper\saveCache
saveCache()
Saves the HTML to the cache in case it got recomputed.
Definition: CacheHelper.php:255
CacheHelper\rebuildOnDemand
rebuildOnDemand()
Rebuild the content, even if it's already cached.
Definition: CacheHelper.php:314
IContextSource\getTitle
getTitle()
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:57
CacheHelper
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:45
CacheHelper\$cacheEnabled
bool $cacheEnabled
If the cache is enabled or not.
Definition: CacheHelper.php:79
CacheHelper\$cacheKey
array $cacheKey
Elements to build a cache key with.
Definition: CacheHelper.php:95
IContextSource\getRequest
getRequest()
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1081
CacheHelper\setOnInitializedHandler
setOnInitializedHandler( $handlerFunction)
Sets a function that gets called when initialization of the cache is done.
Definition: CacheHelper.php:325
CacheHelper\__construct
__construct()
Definition: CacheHelper.php:97
IContextSource\getLanguage
getLanguage()
CacheHelper\getCacheKeyString
getCacheKeyString()
Returns the cache key to use to cache this page's HTML output.
Definition: CacheHelper.php:286