4 * Interface for all classes implementing CacheHelper functionality.
8 * @licence GNU GPL v2 or later
9 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
11 interface ICacheHelper
{
14 * Sets if the cache should be enabled or not.
17 * @param boolean $cacheEnabled
19 function setCacheEnabled( $cacheEnabled );
22 * Initializes the caching.
23 * Should be called before the first time anything is added via addCachedHTML.
27 * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
28 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
30 function startCache( $cacheExpiry = null, $cacheEnabled = null );
33 * Get a cached value if available or compute it if not and then cache it if possible.
34 * The provided $computeFunction is only called when the computation needs to happen
35 * and should return a result value. $args are arguments that will be passed to the
36 * compute function when called.
40 * @param {function} $computeFunction
41 * @param array|mixed $args
42 * @param string|null $key
46 function getCachedValue( $computeFunction, $args = array(), $key = null );
49 * Saves the HTML to the cache in case it got recomputed.
50 * Should be called after the last time anything is added via addCachedHTML.
57 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
61 * @param integer $cacheExpiry
63 function setExpirey( $cacheExpiry );
68 * Helper class for caching various elements in a single cache entry.
70 * To get a cached value or compute it, use getCachedValue like this:
71 * $this->getCachedValue( $callback );
73 * To add HTML that should be cached, use addCachedHTML like this:
74 * $this->addCachedHTML( $callback );
76 * The callback function is only called when needed, so do all your expensive
77 * computations here. This function should returns the HTML to be cached.
78 * It should not add anything to the PageOutput object!
80 * Before the first addCachedHTML call, you should call $this->startCache();
81 * After adding the last HTML that should be cached, call $this->saveCache();
85 * @file CacheHelper.php
87 * @licence GNU GPL v2 or later
88 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
90 class CacheHelper
implements ICacheHelper
{
93 * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
98 protected $cacheExpiry = 3600;
101 * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached).
102 * If no cached already, then the newly computed chunks are added here,
103 * if it as cached already, chunks are removed from this list as they are needed.
108 protected $cachedChunks;
111 * Indicates if the to be cached content was already cached.
112 * Null if this information is not available yet.
117 protected $hasCached = null;
120 * If the cache is enabled or not.
125 protected $cacheEnabled = true;
128 * Function that gets called when initialization is done.
133 protected $onInitHandler = false;
136 * Sets if the cache should be enabled or not.
139 * @param boolean $cacheEnabled
141 public function setCacheEnabled( $cacheEnabled ) {
142 $this->cacheEnabled
= $cacheEnabled;
146 * Initializes the caching.
147 * Should be called before the first time anything is added via addCachedHTML.
151 * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
152 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
154 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
155 if ( is_null( $this->hasCached
) ) {
156 if ( !is_null( $cacheExpiry ) ) {
157 $this->cacheExpiry
= $cacheExpiry;
160 if ( !is_null( $cacheEnabled ) ) {
161 $this->setCacheEnabled( $cacheEnabled );
164 $this->initCaching();
169 * Returns a message that notifies the user he/she is looking at
170 * a cached version of the page, including a refresh link.
174 * @param IContextSource $context
178 public function getCachedNotice( IContextSource
$context ) {
179 $refreshArgs = $context->getRequest()->getQueryValues();
180 unset( $refreshArgs['title'] );
181 $refreshArgs['action'] = 'purge';
183 $subPage = $context->getTitle()->getFullText();
184 $subPage = explode( '/', $subPage, 2 );
185 $subPage = count( $subPage ) > 1 ?
$subPage[1] : false;
187 $refreshLink = Linker
::link(
188 $context->getTitle( $subPage ),
189 $context->msg( 'cachedspecial-refresh-now' )->escaped(),
194 if ( $this->cacheExpiry
< 86400 * 3650 ) {
195 $message = $context->msg(
196 'cachedspecial-viewing-cached-ttl',
197 $context->getLanguage()->formatDuration( $this->cacheExpiry
)
201 $message = $context->msg(
202 'cachedspecial-viewing-cached-ts'
206 return $message . ' ' . $refreshLink;
210 * Initializes the caching if not already done so.
211 * Should be called before any of the caching functionality is used.
215 protected function initCaching() {
216 if ( $this->cacheEnabled
&& is_null( $this->hasCached
) ) {
217 $cachedChunks = wfGetCache( CACHE_ANYTHING
)->get( $this->getCacheKeyString() );
219 $this->hasCached
= is_array( $cachedChunks );
220 $this->cachedChunks
= $this->hasCached ?
$cachedChunks : array();
222 if ( $this->onInitHandler
!== false ) {
223 call_user_func( $this->onInitHandler
, $this->hasCached
);
229 * Get a cached value if available or compute it if not and then cache it if possible.
230 * The provided $computeFunction is only called when the computation needs to happen
231 * and should return a result value. $args are arguments that will be passed to the
232 * compute function when called.
236 * @param {function} $computeFunction
237 * @param array|mixed $args
238 * @param string|null $key
242 public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
243 $this->initCaching();
245 if ( $this->cacheEnabled
&& $this->hasCached
) {
248 if ( is_null( $key ) ) {
249 $itemKey = array_keys( array_slice( $this->cachedChunks
, 0, 1 ) );
250 $itemKey = array_shift( $itemKey );
252 if ( !is_integer( $itemKey ) ) {
253 wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__
);
255 elseif ( is_null( $itemKey ) ) {
256 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__
);
259 $value = array_shift( $this->cachedChunks
);
263 if ( array_key_exists( $key, $this->cachedChunks
) ) {
264 $value = $this->cachedChunks
[$key];
265 unset( $this->cachedChunks
[$key] );
268 wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__
);
273 if ( !is_array( $args ) ) {
274 $args = array( $args );
277 $value = call_user_func_array( $computeFunction, $args );
279 if ( $this->cacheEnabled
) {
280 if ( is_null( $key ) ) {
281 $this->cachedChunks
[] = $value;
284 $this->cachedChunks
[$key] = $value;
293 * Saves the HTML to the cache in case it got recomputed.
294 * Should be called after the last time anything is added via addCachedHTML.
298 public function saveCache() {
299 if ( $this->cacheEnabled
&& $this->hasCached
=== false && !empty( $this->cachedChunks
) ) {
300 wfGetCache( CACHE_ANYTHING
)->set( $this->getCacheKeyString(), $this->cachedChunks
, $this->cacheExpiry
);
305 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
309 * @param integer $cacheExpiry
311 public function setExpirey( $cacheExpiry ) {
312 $this->cacheExpiry
= $cacheExpiry;
316 * Returns the cache key to use to cache this page's HTML output.
317 * Is constructed from the special page name and language code.
323 protected function getCacheKeyString() {
324 return call_user_func_array( 'wfMemcKey', $this->cacheKey
);
327 public function setCacheKey( array $cacheKey ) {
328 $this->cacheKey
= $cacheKey;
331 public function purge() {
332 $this->hasCached
= false;
335 public function setOnInitializedHandler( $handlerFunction ) {
336 $this->onInitHandler
= $handlerFunction;