PHP is installed on more than 20m websites, including some of the most popular websites in the world.
It’s easy to install, easy to work with and when put in the hands of a competent developer can achieve the performance and scalability that highly-popular websites and demanding web applications require.
Caching is an important part of a performance and scalability strategy.
In this post, I’ll introduce some of the caching techniques that can be used to boost PHP performance.
When a PHP script is run, the script has to be loaded, parsed, compiled into opcode and then executed. Opcode caching caches the opcode so that the script doesn’t need to be loaded, parsed and compiled the next time it is called.
There are a number of choices when it comes to caching PHP opcode:
While some will recommend one option over the others, my advice is to evaluate each and see which one meets your specific needs and delivers the best results for your application.
An effective caching strategy usually includes some sort of caching of the output of PHP scripts. This is especially beneficial when the output requires data to be retrieved from a database server.
After all, if a database query is made every time a script is run, regardless of whether the results are the same, database load can easily rise quite rapidly if the script is being requested a lot, especially if it involves more intensive queries.
They are easy to work with and well-documented.
For a good introduction to PEAR Cache, check out this tutorial.
Caching Data and Objects
Sometimes caching the output of a PHP script doesn’t go far enough and caching actual data and objects is more desirable.
In this case, let me introduce you to your new best friend – memcached.
Memcached is a free distributed memory object caching system that was originally developed to help scale LiveJournal. Facebook is now its biggest user and reportedly has 200 (or more) dedicated memcached servers with 16GB of RAM each.
There’s even a PECL package that helps PHP developers interface with memcached.