jump to navigation

PHP Accelerators vs Caching PHP December 22, 2010

Posted by Tournas Dimitrios in PHP.

PHP, like all scripting languages, suffers from having to parse all script files each time a page is displayed. Accelerators (opcode caches) are necessary to speed PHP enough for large websites. Introducing the world of PHP accelerators and PHP caching .

PHP Accelerators  (speed boosters ) :

The primary difference between PHP 3 and PHP 4 was that the previous release executed each line of code as it read it in the script, whereas the newer release converted the entire thing to a special internal representation of the code before executing it. The difference is crucial! In PHP 3, each line needed to be read as PHP script, converted into something that can be executed, then executed. If that same line was reached again (eg it was part of a function or inside a loop), it would be read from the script file again, converted into something that could be executed, and executed. As you can see, there was lots of repeated donkey work involved!

In PHP 4, the script is only read once, and converted to an internal representation of it. This results in much faster execution of scripts, particular where repeated code is found. This same paradigm is found in both PHP 4 and 5, and it has the interesting side effect that you can – with the right software – save the compiled version of the PHP script for later use. That is, PHP reads your script, converts it to the internal representation, then saves that representation. Then, when the script is called again, the pre-saved representation is used rather than the original script. This turns out to save a vast amount of processing power, and the best part is that because the PHP is saved before it is executed, pages cached in this way don’t lose any of their “dynamicity” – they can still connect to databases and show user-specific content.

The best part about PHP code caches  is that they automatically check for changes in the original PHP script, so that if you make an edit to the source code, it reaches it automatically. This means that from both a user and developer point of view, you can’t notice it’s there, apart from the speed up.

There are a dozen of free and commercial PHP accelerators available , like :

  • XCache : free
  • Alternative PHP Cache (APC) : free
  • Zend Accelerator : commercial
  • see this list from wikipedia

Caching PHP :

PHP speed booster like the previous example (often called “soft” cachers) work by caching your PHP code before it is executed, which provides a substantial speed boost. But what do you do if the PHP processing itself is taking longer than is acceptable? The answer is usually to cache the output of your pages after they have been output as HTML, a process often known as hard caching, which has two side effects.

Firstly, it will mean your pages will be static most of the time. This is obviously not an option for sites that need dynamic content all the time, such as shopping baskets and the like, but it is definitely possible for news sites where the content only changes once every five minutes. Even on message boards, it is possible to print a message such as “your post will be displayed within the next five minutes”, like Slashdot does.

Secondly, it will mean you get the biggest possible speed boost, because you are only outputting HTML for the majority of the time. All PHP has to do is run a check along the lines of “if content is more than five minutes old, regenerate it, otherwise send the old stuff”.

To implement this system, you need to modify your PHP scripts so that they check for the cached copy being out of date, and generate a new version if needed.


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s