CodeCache technology

pp3345 edited this page Jul 21, 2012 · 3 revisions

A special thing about the Pancake PHP-SAPI is the CodeCache technology. Every experienced PHP developer knows that every bigger PHP-software needs to load the whole Code again and again on every single request. This takes lots of performance that could be better used for actually executing the script. But there is a solution: CodeCache. With Pancakes' CodeCache technology it is possible to keep non-executing source code like class or function definitions in a kind of cache so that this code does not need to be loaded on every request. It is even possible to keep data like objects cached, for example in static class properties of cached classes as these properties aren't (necessarily) deleted after finishing the request.

Using the CodeCache

Using the CodeCache is easy. For example, let's say we have such a file:

<?php
class MyClass {
    public $property = 0;
    private static $instance = null;

    private function __construct() {
        $this->property = rand();
    }

    public static function getInstance() {
        if(!self::$instance)
            self::$instance = new self;
        return self::$instance;
    }
}
?>

The vHosts' document root is /var/vhosts/myproject.com/ and the file is located in /var/vhosts/myproject.com/includes/myClass.php. To cache the file containing the class, just set the vHosts' configuration value PHPCache to

- /includes/myClass.php

Restart Pancake and you'll find that the class is cached and you don't need to load it yourself on a request anymore. Now you've already gained performance. But you can even do more. By default the configuration value PHPResetClassStaticObjects is set to true. This means, in this example, that the singleton instance of MyClass is deleted after every request. This might be useful in some cases and some CMS-systems even require that this setting stays true to work correctly. But if you're developing your own project and you want to give it some optimisations that can be used with Pancake, you can set this to false and your instance will stay alive for multiple requests. This of course does not necessarily mean that your software won't run anymore with other SAPIs (except if you really concept your software only to run with Pancake).

Setting up CodeCache for existing websites

If you already have a PHP-powered website and are migrating to Pancake you might want to optimize it using the CodeCache. But this can be difficult, especially when you are using third party softwares like Joomla or MyBB. So how do we solve this?

First, we start Pancake in debug mode. This can be done via the configuration or the start option --debug. When debugging is enabled, we can use the GET-parameter ?pancakephpdebug. So open any of your websites' pages and append ?pancakephpdebug to the URI (for example: http://mydomain.com/forum/showforum.php?id=7&pancakephpdebug). This will display some useful information instead of the actually requested page, containing included files, defined classes and so on.

A good approach on defining the rules for the CodeCache now is taking the list of included files and trying to add them to the CodeCache. Of course you should not cache files that actually execute code, only files that are defining classes or functions. You can add some files and then test if the page is still working as it might happen that the software reacts allergic on having static values unresetted on a new request.