Memcached and APC: Two Simple Techniques to Speed up your PHP Webpages· 2 minute read
Memcached and APC are two tools that you can install on your server and gain almost instant gratification! APC basically caches executions that you send to a PHP process so that the next time you ask the parser to run your script it only has to look for some pre-chewed opcode in memory rather than parsing your PHP from the disk. APC also has another feature up its sleeve, memory object caching, which allows you to store objects such as results from a database table in memory. Memcached works in much the same way, but it can be used as a session handler as well as a persistent store for object data. Another advantage memcached has over APC is that it can be distributed so you could have a number of servers all maintaining memcached daemons to distribute the load.
APC is faster as a straight PHP call on the memory cache, but you can only access the memory cache from the local PHP process. I have not tested it but I am unsure if that means you can access it from CLI PHP as well as via PHP running on Apache. Often I will have a PHP frontend with any server scripts (for example cronjobs) written in Python so it is very handy to be able to access the cache from a central location, which with memcached is possible but not with APC as it is PHP specific. For more information on performance and advice on when various caching methods may be more useful Peter over at MySQLPerformanceBlog has written an interest article Cache Performance Comparison.
So for code that I know will only ever be used by the local PHP process I store the object in the APC memory object store, but if I need portability or the server is under high load and I want to separate out my object cache server from my script server then I use memcached.
I have written two articles detailing the steps involved in installing both the binaries on RedHat Enterprise Linux and Ubuntu or Debian. Currently the live servers I use are either running CentOS or RedHat and the local development server is very nice setup based upon Ubuntu.
The MySQL documentation contains a very nice set of examples and documentation on using memcached to reduce hits on the database, which includes examples in a few languages including Python and PHP. There is also some very handy hints to be gleened from An Introduction to memcached by Jeremy Ashcrafta.k.aMrSpooky over at Search-This.