As we know, PHP is an interpreted language. The PHP interpreter compiles your scripts each time you call them. However, the
PHP code itself may not change that often in your scripts. Having to compile the code on each call wastes your CPU's time. That's where the idea of caching the
PHP code (and not only the HTML content) comes in (see How to accelerate
PHP-Nuke).
The idea of PHP code caching lies in the basis of many commercial PHP accelerators, like
Hopefully, there is a free open source alternative: Turck MMCache, a product under the GPL Licence.
Turck MMCache is a free open source PHP accelerator, optimizer, encoder and dynamic content cache for PHP. It increases
performance of PHP scripts by caching them in compiled state, so that the overhead of compiling is almost completely eliminated. Also it uses some optimizations to
speed up execution of PHP scripts. Turck MMCache typically reduces server load and increases the speed of your PHP code by 1-10
times!
Download the newest version (currently 2.4.6) from the Turck MMCache download page at Sourceforge. There are
precompiled binaries for Windows, as well as source packages for Linux and other operating systems.
Before you rush and download it though, let me bring to your attention that the approach taken by a code accelerator like MMCache requires that you install it as a so-called "PHP extension". You
will also be configuring options like the amount of shared memory that you want allocated to MMCache, in order to use it for caching. These steps are normally beyond the reach of mass hosting
accounts on ISPs, so you may be out of luck. However, if you own the web server, or have administration access to it, then MMCache is for you - rush to the download page and get it!
In the following, I will describe the installation process of Turck MMCache for Linux:
-
Login as root.
-
Change to a directory that is appropriate for holding source code. In Linux, this is /usr/src:
-
Extract the package:
tar -xzvf /usr/cdimage/source/turck-mmcache-2.4.6.tar.gz
|
-
It creates an folder with all files. Change to that folder:
-
Find the place where the phpize program is installed on your system. For this, type:
-
Mine is on /usr/bin, as you can see from the output:
# which phpize
/usr/bin/phpize
|
-
so what is before the "/bin" is my PHP_PREFIX. In this case "/usr". Type the following:
export PHP_PREFIX="/usr"
$PHP_PREFIX/bin/phpize
|
You get the output:
autoheader: `config.h.in' is created
You should update your `aclocal.m4' by running aclocal.
|
so you run
-
Now you are ready to create the Makefile with the configure script:
./configure --enable-mmcache=shared --with-php-config=$PHP_PREFIX/bin/php-config
|
-
After that, the Makefile is there and you can do (you must be root for the last step)
This will finish installation of Turck MMCache on your server.
After installation, you need to configure MMCache:
Open the php.ini file (/etc/php.ini), find the "extensions" section and put the following there:
[extension section]
... various other extensions
extension="mmcache.so"
mmcache.shm_size="16"
mmcache.cache_dir="/tmp/mmcache"
mmcache.enable="1"
mmcache.optimizer="1"
mmcache.check_mtime="1"
mmcache.debug="0"
mmcache.filter=""
mmcache.shm_max="0"
mmcache.shm_ttl="0"
mmcache.shm_prune_period="0"
mmcache.shm_only="0"
mmcache.compress="0"
; end of extension section
|
MMCache uses shared memory for caching, as opposed to disk space. This is a reason why it is so fast. The mmcache.shm_size option defines how much shared memory (in MBytes) it should use for
caching.
Again, you need a cache directory too, which is what mmcache.cache_dir is for. Again, the cache directory should be writable and readable by your web server.
You can read about the other options in the Turck MMCache Homepage. For our purposes, you can leave them at their values as in the
example above.
This completes the configuration of MMCache. We still have to talk about its use - but that's easy, see the next section. 
 |
Turck MMCache on a virtual server |
|
Question: I am on a virtual server and should not interfere with the other domains hosted there. How do I configure MMCache in this situation?
Answer: Compile MMCache and put it into the extensions directory. Configure php.ini to utilize the extension but set mmcache.optimizer, .compress and .enable all to FALSE.
Then a .htaccess file (see Section 25.4) in your own webspace and use:
php_value 'mmcache.enable' '1'
php_value 'mmcache.optimizer' '1'
php_value 'mmcache.compress' 1'
|
This should do the trick.
|
By following the configuartion steps above, MMCache already works for you: it compiles PHP code and caches it. Whenever a script's code changes, it recompiles it
and caches it again. You should already see a noticeable improvement in your response times because of this.
But there is more you can do with MMCache: you can cache a whole page, like with jpcache (Section 24.1.1), or individual parts of them, like with Cache-Lite (Section 24.1.2). Since jpcache uses disk space for caching the whole page, you can replace it by MMCache's functionality, which uses shared memory and could thus be up to 5
times faster. Here's how you do it (see How to accelerate PHP-Nuke):
Open the index.php file and change the code we put there for jpcache (see a previous post) as follows:
require_once("mainfile.php");
if (!isset($user)) {
$cachetimeout=900;
mmcache_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET), $cachetimeout);
} else {
$cachetimeout=-1;
}
# require_once "includes/jpcache/jpcache.php";
|
As you can see, we commented the jpcache inclusion line and put a call to mmcache_cache_page with the same cache timeout. This will do exactly what jpcache was doing previously - cache the whole
page in case the user is not logged-in and leave it uncached in case he is, so that Cache-Lite can cache parts of it. But due to the shared memory implementation, we are going the get an extra speed
kick out of it. 
mmcache_cache_page is only one of many MMCache functions you can use in your blocks and modules to speed up things. Some other interesting ones are:
-
mmcache_set_session_handlers(): install the MMCache session handlers. Since PHP 4.2.0 you can install MMCache session handlers in "php.ini" by
session.save_handler=mmcache
|
-
mmcache_cache_output($key, $eval_code, $ttl=0): caches the output of $eval_code in shared memory for $ttl seconds. Output can be removed from cache by calling mmcach_rm() with the same $key. For
Example:
<?php mmcache_cache_output('test', 'echo time(); phpinfo();', 30); ?>
|
-
mmcache_cache_result($key, $eval_code, $ttl=0): caches the result of $eval_code in shared memory for $ttl seconds. Result can be removed from cache by calling mmcach_rm() with the same $key. For
Example:
<?php mmcache_cache_output('test', 'time()." Hello";', 30); ?>
|
There are other uses of MMCache, like its use as an encoder to hide your precious PHP scripts from the spying eyes of competitors - but we are talking open
source here, aren't we? 