Because we encounter PHP more and more in the enterprise market, the performance demands of websites we build are becoming ever more challenging. The usefulness of caching is often underestimated by developers (myself included). When using a lightweight framework like Code Igniter it is easy to assume caching is not needed or not necessary; still there are websites where performance optimization is needed and it is important to think about the performance and caching strategy beforehand. When writing the technical specifications for a high performance website the developer needs to be able to make the best choice for optimization.
For optimizing the performance of a website there are several techniques.The choice of the framework or CMS is important and the performance demands are a factor when determining which framework or CMS will be used, but it doesn’t stop there.
One of the methods to optimize the performance of a website is caching. There are several methods of caching; In this blogpost I will introduce some of the most common ones:
- File caching
- Memory caching
- The MySQL Query Cache
The idea of file caching is that you cache the complete HTML of a page or a section of the page in a file on the harddisk of the server. If the page or section is cached and the cache’s ‘lifetime’ is not yet elapsed the HTML from the file cache is included. This solution requires some harddisk space for storing the files. There is also a small overhead for reading and writing the cache files.
Memory caching can be achieved with several third party tools. One of these is ‘memcache’. The memcache solution is a separate PHP module which can be installed as an extension. It requires PHP version 4.3.3 or later. The PHP memcache module is a frontend to the third party software “memcached”. It is originally designed to cache the result of database queries in the memory, but can also be used to cache other data. Memcached needs one or more processes on the server, each process allocates memory of the server with a maximum of 4GB. These processes can be used to cache query results or even complete HTML files. It depends on the amount of available memory and caching you use if this is a suitable solution. Each process has its own memory limit and caching is not possible if the memory is full. There are also other solutions to use memory based caching, such as the Zend Platform or APC.
The MySQL Query Cache
The MySQL query cache is built-in functionality of MySQL to cache queries in the memory of the server. For every query MySQL determines if the query is already cached or not. This means the complete query needs to be exactly the same to benefit from this, otherwise MySQL will think it’s a different query. So it’s very well suited for scripts that perform the same query over and over again.
The amount of memory used for caching queries is configurable. The MySQL query cache cannot cache stored procedures, so this solution only works for SELECT queries. Since the query cache only caches queries and often you need more caching than just the queries, this solution should typically be used in combination with one of the above caching methods. More information can be found here and here.
Several frameworks and CMS systems have a built in caching solution, these are mostly file caching solutions. For example our own CMS, Code igniter and WordPress (with an extra plugin, freely available) all have file caching solutions. Regardless of the CMS or framework being used, it might be worth having a look at the Zend Platform. Zend Platform supports file caching and memory based caching. There is (usually) no need to change the PHP code of the website to be able to use this caching, since it’s url based. It also supports partial page caching by making several PHP functions available to the developer (this would of course require some changes to the code).
These are just a few caching methods and of course there are other possibilities to optimize the performance of a website. Some complex queries need indexes in the database to perform faster. Also using an ‘accelerator’ is a good option to improve the performance of a website. Not all options are always possible, for example when using a shared hosting environment, there might be restrictions on what you can use. For every project, developers need to determine the best caching strategy. That’s what keeps us challenged!