|Responsibilities||Providing caching techniques in order to optimize the performance of MediaWiki|
|Implementation||Object Caching and Web Caching|
Module cache is responsible for providing caching techniques in order to optimize the performance of MediaWiki. Both object caching and web caching are supported by MediaWiki software. Object caching stores data and single objects, that are frequently requested and require resources to be generated. There are a lot of objects that MediaWiki stores in cache, some of them are presented below:
- Array of data for image (version, size, width, height, metadata etc.)
- Wiki text after xml preprocessing
- Revision text
- Revision differences
Web caching allows storing of entire HTML pages. To support web caching MediaWiki provides integration with the Squid proxy server and Varnish accelerator. It notifies them, when a page should be purged from the cache and regenerated.
Main article: Manual:Object cache.
MediaWiki supports object caching storage in many places, for example, on a file system, in the database or in an external system like memcached.
The storage mode is set by the global variable
$wgMainCacheType in the
Additionally cache type for messages and parser can be specified by setting
The super class for all types of caching is
BagOStuff, it provides interface for such methods as getting an item from cache, adding an item to cache, deleting it etc.
All specific implementations of caching inherit from this class, as shown on figure Supported options for cache storing.
Thus, for example,
SqlBagOStuff provides object caching using SQL database and
APCBagOStuff does so by using PHP's APC accelerator.
$wgMemc is the global variable for accessing the main cache.
It is initialized from
$wgMainCacheType by calling
This initialization returns a specific
BagOStuff object, for instance, in case the
$wgMainCacheType = CACHE_MEMCACHED, it will return a
Further on whenever necessary every object can be stored by calling
wgMemc->set() (providing key and value) and retrieved by calling
wgMemc->get() (providing key).
The key generation for saving in cache has some common rules. It always starts with the name of the database and follows by specification of the type of object that is being cached. Examples of such keys are shown in the table Cache keys. Final part of the key varies. For example, for user object it will be the user's id and for file object it will be the md5 hash of its name. The generation of key for ParserOutput is explained in the documentation for module Parser.
|Wiki text preprocessed in XML||buildings_en:preprocess-xml:7630d77fb556ce2262f9c890a07e869c:0|
Apart from storing objects and data through 'central' caching set up by
$wgMainCacheType variable, MediaWiki implements itself additional types of caching.
LinkCache is used for caching of title objects and their ids, that are linked from a particular source.
For example, an article would have links to other articles, used templates, categories, its talk page etc.
All these links would be stored in a
LinkCache object and retrieved from there when needed.
HTMLFileCache implements caching of entire HTML pages on the file system.
$wgUseFileCache is set to true, pages can be cached on the server and served to users when requested.
LocalizationCache represents an object with cached contents of localization files.
MediaWiki uses so called messages for any user-facing part of the interface.
LocalizationCache holds this kind of messages for loaded languages, and this way they can be easily accessed when producing the customized HTML page.
To provide integration with Squid or Varnish MediaWiki implements the purging notification.
The class responsible for purging Squid URLs given a title (or titles) is
When an HTML page cache needs to be invalidated, an instance of
CdnCacheUpdate is created and method
doUpdate() is called on it.
The overview of different types of caching in the Cache module can be seen on figure Types of cache in MediaWiki.
See Manual:Parser cache.