User:DanielRenfro/memcache

From mediawiki.org

Memcached is a general-purpose distributed memory caching system. It works on a simple key-value system.

Running the memcached server[edit]

First, make sure there isn't already a memcached server running:

 % ps aux | grep memcached

by hand[edit]

You can start memcached by hand if you want to, but it's probably better to start it with the custom script we used. To start a daemon by hand:

memcached -d -l 127.0.0.1 -p 11000 -m 128
memcached -d -l 127.0.0.1 -p 11001 -m 128

memcached help[edit]

In bold are the options we commonly use:

memcached 1.1.12
-p <num>      port number to listen on
-l <ip_addr>  interface to listen on, default is INDRR_ANY
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes, default is 64 MB
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections, default is 1024
-k            lock down all paged memory
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-h            print this help and exit
-i            print memcached and libevent license
-P <file>     save PID in <file>, only used with -d option

In Mediawiki[edit]

To use the memcached client from within MW we need to make use of the global $wgMemc variable.

background[edit]

  • at some point during the initialization of the wiki (per response), the Setup.php file is run, calling wfGetMainCache
  • wfGetMainCache looks to see what type of caching we want (set in $wgMainCacheType) and returns the appropriate object.
  • for CACHE_MEMCACHED, it returns an instance of MemCachedClientForWikis, and sets this to $wgMemc


Setup (in LocalSettings.php)[edit]

It is imperative that memcached be set up correctly in LocalSettings.php if we are going to make use of it.

// ======= Memcache ==================
$wgMainCacheType = CACHE_MEMCACHED;
$wgMemCachedServers = array( "127.0.0.1:11000", "127.0.0.1:11001" );

to use[edit]

See Also[edit]