3 interface ICacheHelper
{
6 * Sets if the cache should be enabled or not.
9 * @param boolean $cacheEnabled
11 function setCacheEnabled( $cacheEnabled );
14 * Initializes the caching.
15 * Should be called before the first time anything is added via addCachedHTML.
19 * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
20 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
22 function startCache( $cacheExpiry = null, $cacheEnabled = null );
25 * Add some HTML to be cached.
26 * This is done by providing a callback function that should
27 * return the HTML to be added. It will only be called if the
28 * item is not in the cache yet or when the cache has been invalidated.
32 * @param {function} $computeFunction
34 * @param string|null $key
36 function addCachedHTML( $computeFunction, $args = array(), $key = null );
39 * Saves the HTML to the cache in case it got recomputed.
40 * Should be called after the last time anything is added via addCachedHTML.
47 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
51 * @param integer $cacheExpiry
53 function setExpirey( $cacheExpiry );
58 * Helper class for caching various elements in a single cache entry.
60 * To get a cached value or compute it, use getCachedValue like this:
61 * $this->getCachedValue( $callback );
63 * To add HTML that should be cached, use addCachedHTML like this:
64 * $this->addCachedHTML( $callback );
66 * The callback function is only called when needed, so do all your expensive
67 * computations here. This function should returns the HTML to be cached.
68 * It should not add anything to the PageOutput object!
70 * Before the first addCachedHTML call, you should call $this->startCache();
71 * After adding the last HTML that should be cached, call $this->saveCache();
75 * @file CacheHelper.php
76 * @ingroup SpecialPage
78 * @licence GNU GPL v2 or later
79 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
81 class CacheHelper
implements ICacheHelper
{
84 * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
89 protected $cacheExpiry = 3600;
92 * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached).
93 * If no cached already, then the newly computed chunks are added here,
94 * if it as cached already, chunks are removed from this list as they are needed.
99 protected $cachedChunks;
102 * Indicates if the to be cached content was already cached.
103 * Null if this information is not available yet.
108 protected $hasCached = null;
111 * If the cache is enabled or not.
116 protected $cacheEnabled = true;
119 * Function that gets called when initialization is done.
124 protected $onInitHandler = false;
127 * Sets if the cache should be enabled or not.
130 * @param boolean $cacheEnabled
132 public function setCacheEnabled( $cacheEnabled ) {
133 $this->cacheEnabled
= $cacheEnabled;
137 * Initializes the caching.
138 * Should be called before the first time anything is added via addCachedHTML.
142 * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
143 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
145 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
146 if ( is_null( $this->hasCached
) ) {
147 if ( !is_null( $cacheExpiry ) ) {
148 $this->cacheExpiry
= $cacheExpiry;
151 if ( !is_null( $cacheEnabled ) ) {
152 $this->setCacheEnabled( $cacheEnabled );
155 $this->initCaching();
160 * Returns a message that notifies the user he/she is looking at
161 * a cached version of the page, including a refresh link.
165 * @param IContextSource $context
169 public function getCachedNotice( IContextSource
$context ) {
170 $refreshArgs = $context->getRequest()->getQueryValues();
171 unset( $refreshArgs['title'] );
172 $refreshArgs['action'] = 'purge';
174 $subPage = $context->getTitle()->getFullText();
175 $subPage = explode( '/', $subPage, 2 );
176 $subPage = count( $subPage ) > 1 ?
$subPage[1] : false;
178 $refreshLink = Linker
::link(
179 $context->getTitle( $subPage ),
180 $context->msg( 'cachedspecial-refresh-now' )->escaped(),
185 if ( $this->cacheExpiry
< 86400 * 3650 ) {
186 $message = $context->msg(
187 'cachedspecial-viewing-cached-ttl',
188 $context->getLanguage()->formatDuration( $this->cacheExpiry
)
192 $message = $context->msg(
193 'cachedspecial-viewing-cached-ts'
197 return $message . ' ' . $refreshLink;
201 * Initializes the caching if not already done so.
202 * Should be called before any of the caching functionality is used.
206 protected function initCaching() {
207 if ( $this->cacheEnabled
&& is_null( $this->hasCached
) ) {
208 $cachedChunks = wfGetCache( CACHE_ANYTHING
)->get( $this->getCacheKeyString() );
210 $this->hasCached
= is_array( $cachedChunks );
211 $this->cachedChunks
= $this->hasCached ?
$cachedChunks : array();
213 if ( $this->onInitHandler
!== false ) {
214 call_user_func( $this->onInitHandler
, $this->hasCached
);
222 * Add some HTML to be cached.
223 * This is done by providing a callback function that should
224 * return the HTML to be added. It will only be called if the
225 * item is not in the cache yet or when the cache has been invalidated.
229 * @param {function} $computeFunction
231 * @param string|null $key
233 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
234 $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) );
238 * Get a cached value if available or compute it if not and then cache it if possible.
239 * The provided $computeFunction is only called when the computation needs to happen
240 * and should return a result value. $args are arguments that will be passed to the
241 * compute function when called.
245 * @param {function} $computeFunction
246 * @param array|mixed $args
247 * @param string|null $key
251 public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
252 $this->initCaching();
254 if ( $this->cacheEnabled
&& $this->hasCached
) {
257 if ( is_null( $key ) ) {
258 $itemKey = array_keys( array_slice( $this->cachedChunks
, 0, 1 ) );
259 $itemKey = array_shift( $itemKey );
261 if ( !is_integer( $itemKey ) ) {
262 wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__
);
264 elseif ( is_null( $itemKey ) ) {
265 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__
);
268 $value = array_shift( $this->cachedChunks
);
272 if ( array_key_exists( $key, $this->cachedChunks
) ) {
273 $value = $this->cachedChunks
[$key];
274 unset( $this->cachedChunks
[$key] );
277 wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__
);
282 if ( !is_array( $args ) ) {
283 $args = array( $args );
286 $value = call_user_func_array( $computeFunction, $args );
288 if ( $this->cacheEnabled
) {
289 if ( is_null( $key ) ) {
290 $this->cachedChunks
[] = $value;
293 $this->cachedChunks
[$key] = $value;
302 * Saves the HTML to the cache in case it got recomputed.
303 * Should be called after the last time anything is added via addCachedHTML.
307 public function saveCache() {
308 if ( $this->cacheEnabled
&& $this->hasCached
=== false && !empty( $this->cachedChunks
) ) {
309 wfGetCache( CACHE_ANYTHING
)->set( $this->getCacheKeyString(), $this->cachedChunks
, $this->cacheExpiry
);
314 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
318 * @param integer $cacheExpiry
320 public function setExpirey( $cacheExpiry ) {
321 $this->cacheExpiry
= $cacheExpiry;
325 * Returns the cache key to use to cache this page's HTML output.
326 * Is constructed from the special page name and language code.
332 protected function getCacheKeyString() {
333 return call_user_func_array( 'wfMemcKey', $this->cacheKey
);
336 public function setCacheKey( array $cacheKey ) {
337 $this->cacheKey
= $cacheKey;
340 public function purge() {
341 $this->hasCached
= false;
344 public function setOnInitializedHandler( $handlerFunction ) {
345 $this->onInitHandler
= $handlerFunction;