Cache Your Zend Framework Config to Improve Performance

Are you trying to squeeze every last drop of performance out of your Zend Framework application? Once you have implemented caching for queries, templates, etc. there is still one other piece of the puzzle you can cache, the application.ini config. For every page load, this config has to be read in and parsed, so it makes sense to cache this. What I’ve done below is to cache the config using APC so that it is even faster than caching to disk.

<?php
...
/** Zend_Application */
require_once 'Zend/Application.php';

// Use default application.ini config the first time
// then pull the php array from apc
$config_file = APPLICATION_PATH . '/configs/application.ini';
$config_cached = false;
if (apc_exists('application.ini')) {
    $config_file = apc_fetch('application.ini');
    $config_cached = true;
}

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    $config_file
);

// cache the application.ini config if this is staging or production
if (!$config_cached && ('development' != APPLICATION_ENV)) {
    apc_add('application.ini', $application->getOptions());
}

Now you may be asking yourself What happens when I push new code to staging or production? If your APC setup is like mine, then apc.stat = 0. This means you have to restart Apache in order to see new code. So when you push code and restart Apache, your new application.ini will be picked up.

Benchmarks to come…

Related Posts