Introduction
Memcached is a simple key/value memory store that allows values from pretty much any application to be stored in memory for quick retrieval. Most languages have simple APIs that make this very easy from a programming standpoint. This also means that it’s not quite as simple to configure as APC (but nearly for modern applications).
Installation and Configuration
To install memcached I recommend using your choice of installation (mine being emerge) and then checking the configuration (again in the case of Gentoo) located at /etc/conf.d/memcached.
Update the file to listen on the interface you want, use the memory you want, etc and then start the daemon up. Now if you’re using memcached on the same server as your apache server most webapps will be able to just turn on memcached and connect and work but if you’re using memcached and apache on separate servers it’s a little more work.
WordPress and memcached
I’ve already touted the wordpress plugin: W3 Total Cache but unfortunately this plugin doesn’t allow you to easily configure memcached usage through the web interface. It defaults to using the memcached server located at 127.0.0.1:11211 which if we’re using separate servers is less than ideal. The file we need to modify with a simply sed (or by hand if you prefer knowing what’s going on) is %{DOCROOT}/wp-content/w3-total-cache-config.php.
We just need to change the memcached servers to point at our server’s location before enabling it in the interface. To easily accomplish this we can use sed:
sed -i -e 's/127.0.0.1:11211/<host>:<port>/g' %{DOCROOT}/wp-content/w3-total-cache-config.php
Mediawiki and memcached
The other application we can easily setup for memcached is mediawiki. Mediawiki wants us to modify its LocalSettings.php with the following additions:
## Shared memory settings
$wgMainCacheType = CACHE_MEMCACHED;
$wgParserCacheType = CACHE_MEMCACHED;
$wgMessageCacheType = CACHE_MEMCACHED;
$wgMemCachedServers = array("giskard.alunduil.com:11211");
$wgSessionsInMemcached = true;
These settings simply turn on memcached (CACHE_MEMCACHED) for the various caching areas that mediawiki uses and specifies an array of memcached servers to store that caching information in.
If you do have more than one memcached server configured and available you can use the following syntax to include all of them with various weights:
$wgMemCachedServers = array(array("host:port", weight), array("host2:port2", weight));
Conclusion
Using memcached can greatly improve the performance of webapps or other applications that don’t need to present the most up to date information. This can reduce traffic to an overloaded database or configuration file or whatever other store you want to place a cache in front of.
Frederick Townes
2010/06/29 at 9:24 PM
Multiple memcached servers can be specified in the input box and separate pools can be used for different caching engines.