Manual:Architectural modules/Cache

From MediaWiki.org
Jump to navigation Jump to search
Module
Cache
Responsibilities Providing caching techniques in order to optimize the performance of MediaWiki
Implementation Object Caching and Web Caching

Responsibilities[edit]

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:

  • User
  • ParserOutput
  • 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.

Implementation Information[edit]

Object Caching[edit]

Supported options for cache storing

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 LocalSettings.php. Additionally cache type for messages and parser can be specified by setting $wgParserCacheType and $wgMessageCacheType. 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 ObjectCache::getInstance($wgMainCacheType). This initialization returns a specific BagOStuff object, for instance, in case the $wgMainCacheType = CACHE_MEMCACHED, it will return a MemcachedPhpBagOStuff. 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.

Cache keys
Object Key
ParserOutput buildings_en:pcache:idhash:2-0!*!*!*!*!*!*
User buildings_en:user:id:1
File buildings_en:file:f9f25d227db94434eb6c42fc6016ed83
Wiki text preprocessed in XML buildings_en:preprocess-xml:7630d77fb556ce2262f9c890a07e869c:0
Revisions differences buildings_en:diff:version:1.11a:oldid:139:newid:140

Apart from storing objects and data through 'central' caching set up by $wgMainCacheType variable, MediaWiki implements itself additional types of caching. For example LinkCache, HTMLFileCache or LocalizationCache.

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.

Types of cache in MediaWiki

HTMLFileCache implements caching of entire HTML pages on the file system. If $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. For instance, a message 'privacy' would have value 'Privacy policy' for English and 'Politica de proteccion de datos' for Spanish language. LocalizationCache holds this kind of messages for loaded languages, and this way they can be easily accessed when producing the customized HTML page.

Web Caching[edit]

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 SquidUpdate. When an HTML page cache needs to be invalidated, an instance of SquidUpdate 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.

MediaWiki • Page • Action • Parser • Cache • OutputPage • Skin • Further modules