Speed up PHP (Advanced)

OP Code Caching

When you excute a php script it has to be compiled and run. If you install a PHP OP Code cache, the code is stored in it’s compiled state so compiling is then skipped for the next page request. This is particularly useful if you have a PHP script that does a lot of calculations or loops.

Speed improvements can range from 40% to over 150%. Installing one is easy so it should be a feature which is used by all PHP programmers.

Here are some of the current contenders that may be of interest : eAccelerator, X-Cache, MemCache, APC

Database Caching

The products above store the compiled state of the PHP, but this does not help us if we have a slow database query. To combat this we store the database results into a file on the local system and call it instead of the database. To make sure the content gets appropriately updated the cache should be regenerate often.

Here is a pseudo code example:

retrieve cache item

if cache item doesn’t exist then

load database results

store cache item

endif

use cache item

You can also check to see how old the cache is : ex : 30 minutes and have it regenerate automatically.

2 Servers are Better than 1

Usually on a web host you will have 1 server processing all the requests (typically Apache). A good way to optimise the setup is a to add an additional “lighter” server to handle requests for static files like : images, javascript, etc … then leave all the heavy requests (parsing of PHP files) to Apache. Now that Apache doesn’t have to waste resourcing on images and other files it will have more resources available for processing.


Other Items of Interest

jMeter – Apache benchmarking tool that records user movement and replicates it.

nGinx – A high performance web server.