MediaWiki
1.28.0
|
Helper class for caching various elements in a single cache entry. More...
Public Member Functions | |
getCachedNotice (IContextSource $context, $includePurgeLink=true) | |
Returns a message that notifies the user he/she is looking at a cached version of the page, including a refresh link. More... | |
getCachedValue ($computeFunction, $args=[], $key=null) | |
Get a cached value if available or compute it if not and then cache it if possible. More... | |
rebuildOnDemand () | |
Rebuild the content, even if it's already cached. More... | |
saveCache () | |
Saves the HTML to the cache in case it got recomputed. More... | |
setCacheEnabled ($cacheEnabled) | |
Sets if the cache should be enabled or not. More... | |
setCacheKey (array $cacheKey) | |
Sets the cache key that should be used. More... | |
setExpiry ($cacheExpiry) | |
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry... More... | |
setOnInitializedHandler ($handlerFunction) | |
Sets a function that gets called when initialization of the cache is done. More... | |
startCache ($cacheExpiry=null, $cacheEnabled=null) | |
Initializes the caching. More... | |
Protected Member Functions | |
getCacheKeyString () | |
Returns the cache key to use to cache this page's HTML output. More... | |
initCaching () | |
Initializes the caching if not already done so. More... | |
Protected Attributes | |
array | $cachedChunks |
List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached). More... | |
bool | $cacheEnabled = true |
If the cache is enabled or not. More... | |
int | $cacheExpiry = 3600 |
The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry. More... | |
array | $cacheKey = [] |
Elements to build a cache key with. More... | |
bool null | $hasCached = null |
Indicates if the to be cached content was already cached. More... | |
callable | $onInitHandler = false |
Function that gets called when initialization is done. More... | |
Helper class for caching various elements in a single cache entry.
To get a cached value or compute it, use getCachedValue like this: $this->getCachedValue( $callback );
To add HTML that should be cached, use addCachedHTML like this: $this->addCachedHTML( $callback );
The callback function is only called when needed, so do all your expensive computations here. This function should returns the HTML to be cached. It should not add anything to the PageOutput object!
Before the first addCachedHTML call, you should call $this->startCache(); After adding the last HTML that should be cached, call $this->saveCache();
Definition at line 103 of file CacheHelper.php.
CacheHelper::getCachedNotice | ( | IContextSource | $context, |
$includePurgeLink = true |
|||
) |
Returns a message that notifies the user he/she is looking at a cached version of the page, including a refresh link.
IContextSource | $context | |
bool | $includePurgeLink |
Definition at line 199 of file CacheHelper.php.
References IContextSource\getLanguage(), IContextSource\getRequest(), IContextSource\getTitle(), Linker\link(), and IContextSource\msg().
CacheHelper::getCachedValue | ( | $computeFunction, | |
$args = [] , |
|||
$key = null |
|||
) |
Get a cached value if available or compute it if not and then cache it if possible.
The provided $computeFunction is only called when the computation needs to happen and should return a result value. $args are arguments that will be passed to the compute function when called.
callable | $computeFunction | |
array | mixed | $args | |
string | null | $key |
Implements ICacheHelper.
Definition at line 264 of file CacheHelper.php.
References $args, $value, initCaching(), and wfWarn().
|
protected |
Returns the cache key to use to cache this page's HTML output.
Is constructed from the special page name and language code.
MWException |
Definition at line 346 of file CacheHelper.php.
Referenced by initCaching(), and saveCache().
|
protected |
Initializes the caching if not already done so.
Should be called before any of the caching functionality is used.
Definition at line 237 of file CacheHelper.php.
References CACHE_ANYTHING, getCacheKeyString(), and wfGetCache().
Referenced by getCachedValue(), and startCache().
CacheHelper::rebuildOnDemand | ( | ) |
Rebuild the content, even if it's already cached.
This effectively has the same effect as purging the cache, since it will be overridden with the new value on the next request.
Definition at line 372 of file CacheHelper.php.
CacheHelper::saveCache | ( | ) |
Saves the HTML to the cache in case it got recomputed.
Should be called after the last time anything is added via addCachedHTML.
Implements ICacheHelper.
Definition at line 315 of file CacheHelper.php.
References CACHE_ANYTHING, getCacheKeyString(), and wfGetCache().
CacheHelper::setCacheEnabled | ( | $cacheEnabled | ) |
Sets if the cache should be enabled or not.
bool | $cacheEnabled |
Implements ICacheHelper.
Definition at line 161 of file CacheHelper.php.
References $cacheEnabled.
Referenced by startCache().
CacheHelper::setCacheKey | ( | array | $cacheKey | ) |
Sets the cache key that should be used.
array | $cacheKey |
Definition at line 361 of file CacheHelper.php.
References $cacheKey.
CacheHelper::setExpiry | ( | $cacheExpiry | ) |
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry...
int | $cacheExpiry |
Implements ICacheHelper.
Definition at line 333 of file CacheHelper.php.
References $cacheExpiry.
CacheHelper::setOnInitializedHandler | ( | $handlerFunction | ) |
Sets a function that gets called when initialization of the cache is done.
callable | $handlerFunction |
Definition at line 383 of file CacheHelper.php.
CacheHelper::startCache | ( | $cacheExpiry = null , |
|
$cacheEnabled = null |
|||
) |
Initializes the caching.
Should be called before the first time anything is added via addCachedHTML.
int | null | $cacheExpiry | Sets the cache expiry, either ttl in seconds or unix timestamp. |
bool | null | $cacheEnabled | Sets if the cache should be enabled or not. |
Implements ICacheHelper.
Definition at line 174 of file CacheHelper.php.
References $cacheExpiry, initCaching(), and setCacheEnabled().
|
protected |
List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
If not cached already, then the newly computed chunks are added here, if it as cached already, chunks are removed from this list as they are needed.
Definition at line 120 of file CacheHelper.php.
|
protected |
If the cache is enabled or not.
Definition at line 137 of file CacheHelper.php.
Referenced by setCacheEnabled().
|
protected |
The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
Definition at line 110 of file CacheHelper.php.
Referenced by setExpiry(), and startCache().
|
protected |
Elements to build a cache key with.
Definition at line 153 of file CacheHelper.php.
Referenced by setCacheKey().
|
protected |
Indicates if the to be cached content was already cached.
Null if this information is not available yet.
Definition at line 129 of file CacheHelper.php.
|
protected |
Function that gets called when initialization is done.
Definition at line 145 of file CacheHelper.php.