| 1 | Memcached {#memcached} |
| 2 | ==================================== |
| 3 | |
| 4 | MediaWiki has optional support for memcached, a "high-performance, |
| 5 | distributed memory object caching system". For general information |
| 6 | on it, see: <http://www.danga.com/memcached/> |
| 7 | |
| 8 | Memcached is likely more trouble than a small site will need, but |
| 9 | for a larger site with heavy load, like Wikipedia, it should help |
| 10 | lighten the load on the database servers by caching data and objects |
| 11 | in memory. |
| 12 | |
| 13 | Installation |
| 14 | -------------------------------- |
| 15 | |
| 16 | Packages are available for Fedora, Debian, Ubuntu and probably other |
| 17 | Linux distributions. If there's no package available for your |
| 18 | distribution, you can compile it from source. |
| 19 | |
| 20 | Compilation |
| 21 | -------------------------------- |
| 22 | |
| 23 | * PHP must be compiled with --enable-sockets |
| 24 | * libevent: <http://www.monkey.org/~provos/libevent/> |
| 25 | (as of 2003-08-11, 0.7a is current) |
| 26 | * optionally, epoll-rt patch for Linux kernel: |
| 27 | <http://www.xmailserver.org/linux-patches/nio-improve.html> |
| 28 | * memcached: <http://www.danga.com/memcached/download.bml> |
| 29 | (as of this writing, 1.1.9 is current) |
| 30 | |
| 31 | Memcached and libevent are under BSD-style licenses. |
| 32 | |
| 33 | The server should run on Linux and other Unix-like systems... you |
| 34 | can run multiple servers on one machine or on multiple machines on |
| 35 | a network; storage can be distributed across multiple servers, and |
| 36 | multiple web servers can use the same cache cluster. |
| 37 | |
| 38 | **W A R N I N G ! ! ! ! !** |
| 39 | |
| 40 | Memcached has no security or authentication. Please ensure that your |
| 41 | server is appropriately firewalled, and that the port(s) used for |
| 42 | memcached servers are not publicly accessible. Otherwise, anyone on |
| 43 | the internet can put data into and read data from your cache. |
| 44 | |
| 45 | An attacker familiar with MediaWiki internals could use this to steal |
| 46 | passwords and email addresses, or to make themselves a sysop and |
| 47 | install malicious javascript on the site. There may be other types |
| 48 | of vulnerability, no audit has been done -- so be safe and keep it |
| 49 | behind a firewall. |
| 50 | |
| 51 | **W A R N I N G ! ! ! ! !** |
| 52 | |
| 53 | Setup |
| 54 | -------------------------------- |
| 55 | |
| 56 | If you installed memcached using a distro, the daemon should be started |
| 57 | automatically using |
| 58 | |
| 59 | /etc/init.d/memcached |
| 60 | |
| 61 | To start the daemon manually, use something like: |
| 62 | |
| 63 | memcached -d -l 127.0.0.1 -p 11211 -m 64 |
| 64 | |
| 65 | (to run in daemon mode, accessible only via loopback interface, |
| 66 | on port 11211, using up to 64 MiB of memory) |
| 67 | |
| 68 | In your LocalSettings.php file, set: |
| 69 | |
| 70 | ```php |
| 71 | $wgMainCacheType = CACHE_MEMCACHED; |
| 72 | $wgMemCachedServers = [ "127.0.0.1:11211" ]; |
| 73 | ``` |
| 74 | |
| 75 | The wiki should then use memcached to cache various data. To use |
| 76 | multiple servers (physically separate boxes or multiple caches |
| 77 | on one machine on a large-memory x86 box), just add more items |
| 78 | to the array. To increase the weight of a server (say, because |
| 79 | it has twice the memory of the others and you want to spread |
| 80 | usage evenly), make its entry a subarray: |
| 81 | |
| 82 | ```php |
| 83 | $wgMemCachedServers = [ |
| 84 | "127.0.0.1:11211", # one gig on this box |
| 85 | [ "192.168.0.1:11211", 2 ] # two gigs on the other box |
| 86 | ]; |
| 87 | ``` |
| 88 | |
| 89 | PHP client for memcached |
| 90 | -------------------------------- |
| 91 | |
| 92 | MediaWiki uses a fork of Ryan T. Dean's pure-PHP memcached client. |
| 93 | It also supports the PECL PHP extension for memcached. |
| 94 | |
| 95 | MediaWiki uses the ObjectCache class to retrieve instances of |
| 96 | BagOStuff by purpose, controlled by the following variables: |
| 97 | * $wgMainCacheType |
| 98 | * $wgParserCacheType |
| 99 | * $wgMessageCacheType |
| 100 | |
| 101 | If you set one of these to CACHE_NONE, MediaWiki still creates a |
| 102 | BagOStuff object, but calls it to it are no-ops. If the cache daemon |
| 103 | can't be contacted, it should also disable itself fairly smoothly. |
| 104 | |
| 105 | Keys used |
| 106 | -------------------------------- |
| 107 | |
| 108 | (incomplete, out of date) |
| 109 | |
| 110 | Date Formatter: |
| 111 | |
| 112 | key: $wgDBname:dateformatter |
| 113 | ex: wikidb:dateformatter |
| 114 | stores: a single instance of the DateFormatter class |
| 115 | cleared by: nothing |
| 116 | expiry: one hour |
| 117 | |
| 118 | Difference Engine: |
| 119 | |
| 120 | key: $wgDBname:diff:version:{MW_DIFF_VERSION}:oldid:$old:newid:$new |
| 121 | ex: wikidb:diff:version:1.11a:oldid:1:newid:2 |
| 122 | stores: body of a difference |
| 123 | cleared by: nothing |
| 124 | expiry: one week |
| 125 | |
| 126 | Interwiki: |
| 127 | |
| 128 | key: $wgDBname:interwiki:$prefix |
| 129 | ex: wikidb:interwiki:w |
| 130 | stores: object from the interwiki table of the database |
| 131 | expiry: $wgInterwikiExpiry |
| 132 | cleared by: nothing |
| 133 | |
| 134 | Lag time of the databases: |
| 135 | |
| 136 | key: $wgDBname:lag_times |
| 137 | ex: wikidb:lag_times |
| 138 | stores: array mapping the database id to its lag time |
| 139 | expiry: 5 secondes |
| 140 | cleared by: nothing |
| 141 | |
| 142 | Localisation: |
| 143 | |
| 144 | key: $wgDBname:localisation:$lang |
| 145 | ex: wikidb:localisation:de |
| 146 | stores: array of localisation settings |
| 147 | set in: Language::loadLocalisation() |
| 148 | expiry: none |
| 149 | cleared by: Language::loadLocalisation() |
| 150 | |
| 151 | Message Cache: |
| 152 | |
| 153 | See MessageCache.php. |
| 154 | |
| 155 | Newtalk: |
| 156 | |
| 157 | key: $wgDBname:newtalk:ip:$ip |
| 158 | ex: wikidb:newtalk:ip:123.45.67.89 |
| 159 | stores: integer, 0 or 1 |
| 160 | set in: User::loadFromDatabase() |
| 161 | cleared by: User::saveSettings() # ? |
| 162 | expiry: 30 minutes |
| 163 | |
| 164 | Parser Cache: |
| 165 | |
| 166 | access: ParserCache |
| 167 | backend: $wgParserCacheType |
| 168 | key: $wgDBname:pcache:idhash:$pageid-$renderkey!$hash |
| 169 | $pageid: id of the page |
| 170 | $renderkey: 1 if action=render, 0 otherwise |
| 171 | $hash: hash of user options applied to the page, see ParserOptions::optionsHash() |
| 172 | ex: wikidb:pcache:idhash:1-0!1!0!!en!2 |
| 173 | stores: ParserOutput object |
| 174 | modified by: WikiPage::doEditUpdates() or PoolWorkArticleView::doWork() |
| 175 | expiry: $wgParserCacheExpireTime or less if it contains short lived functions |
| 176 | |
| 177 | key: $wgDBname:pcache:idoptions:$pageid |
| 178 | stores: CacheTime object with an additional list of used options for the hash, |
| 179 | serves as ParserCache pointer. |
| 180 | modified by: ParserCache::save() |
| 181 | expiry: The same as the ParserCache entry it points to. |
| 182 | |
| 183 | Ping limiter: |
| 184 | |
| 185 | controlled by: $wgRateLimits |
| 186 | key: $wgDBname:limiter:action:$action:ip:$ip, |
| 187 | $wgDBname:limiter:action:$action:user:$id, |
| 188 | mediawiki:limiter:action:$action:ip:$ip and |
| 189 | mediawiki:limiter:action:$action:subnet:$sub |
| 190 | ex: wikidb:limiter:action:edit:ip:123.45.67.89, |
| 191 | wikidb:limiter:action:edit:user:1012 |
| 192 | mediawiki:limiter:action:edit:ip:123.45.67.89 and |
| 193 | mediawiki:limiter:action:$action:subnet:123.45.67 |
| 194 | stores: number of action made by user/ip/subnet |
| 195 | cleared by: nothing |
| 196 | expiry: expiry set for the action and group in $wgRateLimits |
| 197 | |
| 198 | |
| 199 | Proxy Check: (deprecated) |
| 200 | |
| 201 | key: $wgDBname:proxy:ip:$ip |
| 202 | ex: wikidb:proxy:ip:123.45.67.89 |
| 203 | stores: 1 if the ip is a proxy |
| 204 | cleared by: nothing |
| 205 | expiry: $wgProxyMemcExpiry |
| 206 | |
| 207 | Revision text: |
| 208 | |
| 209 | key: $wgDBname:revisiontext:textid:$id |
| 210 | ex: wikidb:revisiontext:textid:1012 |
| 211 | stores: text of a revision |
| 212 | cleared by: nothing |
| 213 | expiry: $wgRevisionCacheExpiry |
| 214 | |
| 215 | Sessions: |
| 216 | |
| 217 | controlled by: $wgSessionsInObjectCache |
| 218 | key: $wgBDname:session:$id |
| 219 | ex: wikidb:session:38d7c5b8d3bfc51egf40c69bc40f8be3 |
| 220 | stores: $SESSION, useful when using a multi-sever wiki |
| 221 | expiry: one hour |
| 222 | cleared by: session_destroy() |
| 223 | |
| 224 | Sidebar: |
| 225 | |
| 226 | access: WANObjectCache |
| 227 | controlled by: $wgEnableSidebarCache |
| 228 | key: $wgDBname:sidebar |
| 229 | ex: wikidb:sidebar |
| 230 | stores: the html output of the sidebar |
| 231 | expiry: $wgSidebarCacheExpiry |
| 232 | cleared by: MessageCache::replace() |
| 233 | |
| 234 | Special:Allpages: |
| 235 | |
| 236 | key: $wgDBname:allpages:ns:$ns |
| 237 | ex: wikidb:allpages:ns:0 |
| 238 | stores: array of pages in a namespace |
| 239 | expiry: one hour |
| 240 | cleared by: nothing |
| 241 | |
| 242 | Special:Recentchanges (feed): |
| 243 | |
| 244 | backend: $wgMessageCacheType |
| 245 | key: $wgDBname:rcfeed:$format:$limit:$hideminor:$target and |
| 246 | rcfeed:$format:timestamp |
| 247 | ex: wikidb:rcfeed:rss:50:: and rcfeed:rss:timestamp |
| 248 | stores: xml output of feed |
| 249 | expiry: one day |
| 250 | clear by: maintenance/rebuildrecentchanges.php script, or |
| 251 | calling Special:Recentchanges?action=purge&feed=rss, |
| 252 | Special:Recentchanges?action=purge&feed=atom, |
| 253 | but note need $wgGroupPermissions[...]['purge'] permission. |