4 * Abstract special page class with scaffolding for caching the HTML output.
6 * Before using any of the cahing functionality, call startCache.
7 * After the last call to either getCachedValue or addCachedHTML, call saveCache.
9 * To get a cached value or compute it, use getCachedValue like this:
10 * $this->getCachedValue( $callback );
12 * To add HTML that should be cached, use addCachedHTML like this:
13 * $this->addCachedHTML( $callback );
15 * The callback function is only called when needed, so do all your expensive
16 * computations here. This function should returns the HTML to be cached.
17 * It should not add anything to the PageOutput object!
21 * @file SpecialCachedPage.php
22 * @ingroup SpecialPage
24 * @licence GNU GPL v2 or later
25 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
27 abstract class SpecialCachedPage
extends SpecialPage
implements ICacheHelper
{
30 * CacheHelper object to which we foreward the non-SpecialPage specific caching work.
31 * Initialized in startCache.
36 protected $cacheHelper;
39 * Sets if the cache should be enabled or not.
42 * @param boolean $cacheEnabled
44 public function setCacheEnabled( $cacheEnabled ) {
45 $this->cacheHelper
->setCacheEnabled( $cacheEnabled );
49 * Initializes the caching.
50 * Should be called before the first time anything is added via addCachedHTML.
54 * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
55 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
57 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
58 $this->cacheHelper
= new CacheHelper( $this->get
);
60 $this->cacheHelper
->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
62 $keyArgs = $this->getCacheKey();
64 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
65 unset( $keyArgs['action'] );
68 $this->cacheHelper
->setCacheKey( $keyArgs );
70 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
71 $this->cacheHelper
->purge();
74 $this->cacheHelper
->startCache( $cacheExpiry, $cacheEnabled );
78 * Add some HTML to be cached.
79 * This is done by providing a callback function that should
80 * return the HTML to be added. It will only be called if the
81 * item is not in the cache yet or when the cache has been invalidated.
85 * @param {function} $computeFunction
87 * @param string|null $key
89 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
90 $this->getOutput()->addHTML( $this->cacheHelper
->getCachedValue( $computeFunction, $args, $key ) );
94 * Saves the HTML to the cache in case it got recomputed.
95 * Should be called after the last time anything is added via addCachedHTML.
99 public function saveCache() {
100 $this->cacheHelper
->saveCache();
104 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
108 * @param integer $cacheExpiry
110 public function setExpirey( $cacheExpiry ) {
111 $this->cacheHelper
->setExpirey( $cacheExpiry );
115 * Returns the variables used to constructed the cache key in an array.
121 protected function getCacheKey() {
124 $this->getLanguage()->getCode()
129 * Gets called after the cache got initialized.
133 * @param boolean $hasCached
135 public function onCacheInitialized( $hasCached ) {
137 $this->getOutput()->setSubtitle( $this->cacheHelper
->getCachedNotice( $this->getContext() ) );