MediaWiki REL1_34
CacheHelper Class Reference

Helper class for caching various elements in a single cache entry. More...

Inheritance diagram for CacheHelper:
Collaboration diagram for CacheHelper:

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.
 
 getCachedValue ( $computeFunction, $args=[], $key=null)
 Get a cached value if available or compute it if not and then cache it if possible.
 
 rebuildOnDemand ()
 Rebuild the content, even if it's already cached.
 
 saveCache ()
 Saves the HTML to the cache in case it got recomputed.
 
 setCacheEnabled ( $cacheEnabled)
 Sets if the cache should be enabled or not.
 
 setCacheKey (array $cacheKey)
 Sets the cache key that should be used.
 
 setExpiry ( $cacheExpiry)
 Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry...
 
 setOnInitializedHandler ( $handlerFunction)
 Sets a function that gets called when initialization of the cache is done.
 
 startCache ( $cacheExpiry=null, $cacheEnabled=null)
 Initializes the caching.
 

Protected Member Functions

 getCacheKeyString ()
 Returns the cache key to use to cache this page's HTML output.
 
 initCaching ()
 Initializes the caching if not already done so.
 

Protected Attributes

array $cachedChunks
 List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
 
bool $cacheEnabled = true
 If the cache is enabled or not.
 
int $cacheExpiry = 3600
 The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
 
array $cacheKey = []
 Elements to build a cache key with.
 
bool null $hasCached = null
 Indicates if the to be cached content was already cached.
 
callable null $onInitHandler
 Function that gets called when initialization is done.
 

Detailed Description

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();

Since
1.20

Definition at line 45 of file CacheHelper.php.

Member Function Documentation

◆ getCachedNotice()

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.

Since
1.20
Parameters
IContextSource$context
bool$includePurgeLink
Returns
string

Definition at line 141 of file CacheHelper.php.

References $context.

◆ getCachedValue()

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.

Since
1.20
Parameters
callable$computeFunction
array | mixed$args
string | null$key
Returns
mixed

Implements ICacheHelper.

Definition at line 206 of file CacheHelper.php.

References $args, initCaching(), and wfWarn().

◆ getCacheKeyString()

CacheHelper::getCacheKeyString ( )
protected

Returns the cache key to use to cache this page's HTML output.

Is constructed from the special page name and language code.

Since
1.20
Returns
string
Exceptions
MWException

Definition at line 286 of file CacheHelper.php.

Referenced by initCaching(), and saveCache().

◆ initCaching()

CacheHelper::initCaching ( )
protected

Initializes the caching if not already done so.

Should be called before any of the caching functionality is used.

Since
1.20

Definition at line 179 of file CacheHelper.php.

References $cachedChunks, CACHE_ANYTHING, getCacheKeyString(), and wfGetCache().

Referenced by getCachedValue(), and startCache().

◆ rebuildOnDemand()

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.

Since
1.20

Definition at line 314 of file CacheHelper.php.

◆ saveCache()

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.

Since
1.20

Implements ICacheHelper.

Definition at line 255 of file CacheHelper.php.

References CACHE_ANYTHING, getCacheKeyString(), and wfGetCache().

◆ setCacheEnabled()

CacheHelper::setCacheEnabled ( $cacheEnabled)

Sets if the cache should be enabled or not.

Since
1.20
Parameters
bool$cacheEnabled

Implements ICacheHelper.

Definition at line 103 of file CacheHelper.php.

References $cacheEnabled.

Referenced by startCache().

◆ setCacheKey()

CacheHelper::setCacheKey ( array $cacheKey)

Sets the cache key that should be used.

Since
1.20
Parameters
array$cacheKey

Definition at line 303 of file CacheHelper.php.

References $cacheKey.

◆ setExpiry()

CacheHelper::setExpiry ( $cacheExpiry)

Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry...

Since
1.20
Parameters
int$cacheExpiry

Implements ICacheHelper.

Definition at line 273 of file CacheHelper.php.

References $cacheExpiry.

◆ setOnInitializedHandler()

CacheHelper::setOnInitializedHandler ( $handlerFunction)

Sets a function that gets called when initialization of the cache is done.

Since
1.20
Parameters
callable$handlerFunction

Definition at line 325 of file CacheHelper.php.

◆ startCache()

CacheHelper::startCache ( $cacheExpiry = null,
$cacheEnabled = null )

Initializes the caching.

Should be called before the first time anything is added via addCachedHTML.

Since
1.20
Parameters
int | null$cacheExpirySets the cache expiry, either ttl in seconds or unix timestamp.
bool | null$cacheEnabledSets if the cache should be enabled or not.

Implements ICacheHelper.

Definition at line 116 of file CacheHelper.php.

References $cacheEnabled, $cacheExpiry, initCaching(), and setCacheEnabled().

Member Data Documentation

◆ $cachedChunks

array CacheHelper::$cachedChunks
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.

Since
1.20

Definition at line 62 of file CacheHelper.php.

Referenced by initCaching().

◆ $cacheEnabled

bool CacheHelper::$cacheEnabled = true
protected

If the cache is enabled or not.

Since
1.20

Definition at line 79 of file CacheHelper.php.

Referenced by setCacheEnabled(), and startCache().

◆ $cacheExpiry

int CacheHelper::$cacheExpiry = 3600
protected

The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.

Since
1.20

Definition at line 52 of file CacheHelper.php.

Referenced by setExpiry(), and startCache().

◆ $cacheKey

array CacheHelper::$cacheKey = []
protected

Elements to build a cache key with.

Since
1.20

Definition at line 95 of file CacheHelper.php.

Referenced by setCacheKey().

◆ $hasCached

bool null CacheHelper::$hasCached = null
protected

Indicates if the to be cached content was already cached.

Null if this information is not available yet.

Since
1.20

Definition at line 71 of file CacheHelper.php.

◆ $onInitHandler

callable null CacheHelper::$onInitHandler
protected

Function that gets called when initialization is done.

Since
1.20

Definition at line 87 of file CacheHelper.php.


The documentation for this class was generated from the following file: