Caching of Zend Framework application configuration file

If you think that you've done everything in terms of performance optimization of your Zend Framework-based project, I bet that your application configuration file was not included in that process. That's the one whose path you supply when instantiating Zend_Application in your index.php. It is usually called “application” and it is written in INI format, but it can be, for example, foo.xml as well. Reason why I'm referring to application config is that its parsing is performed on every request, which is certainly unnecessary, as you don't make configuration changes very often. Solution is simple – cache it.

I've seen many procedural-style solutions for achieving that goal, where caching is implemented early in index.php, using combination of, for example, apc_*() functions. I like doing things OOP-way, so I'll rather create custom Zend_Application class, which is extended with capability of caching supplied configuration file. Here is how such class might look like:

As you can see, constructor now accepts third, optional argument, through which cache instance can be injected. And as Zend_Application::_loadConfig() method is in charge for loading configuration files, idea is to override that method, and customize it, so that it contain caching logic, too.

PHP-based config files (those with .php and .inc extension) are not cached, as no heavy operation is performed for their loading. Their content is simply included. Next, file modification time is retrieved using filemtime() function. It will be used for comparing against the last modified time of a cache entry, retrieved using Zend_Cache_Core::test() method. This is done so that some changes in your config file are automatically effective, without needing to clear its cache entry, created by this class.

To utilize this custom Zend_Application class, make appropriate changes in your index.php:

Note that our custom class will not be caching configuration files if $_configCache is not set. In this example, I'm creating cache instance only if we're in production environment. Also note that I've used Apc on backend, but of course, you can use any of supported backends.

Once you have implemented caching for your SQL queries, pages, and other stuff, boost your performance little more, by doing the same for your application configuration.