ZF2 Cache #1: Caching your config
Introduction
In this post, I intend to demonstrate a small piece of ZF2's caching capabilities. I have used a few of Zend's caching techniques to reduce the considerable overhead produced by ZF2's MVC implementation. These include: caching the application and module configurations, class/template mapping and finally, full-page caching. Today we will start with the easiest; caching configuration.
Caching your configuration files
So, your ZF2 app is complete and running great on your local and stage servers. This is expected with only you, and maybe a handful of testers using the application. With your application ready for live deployment, you may be thinking how you can shave some milliseconds from your page load?
For every HTTP request sent to your ZF2 app, the configuration file (module.config.php) for each of your modules and the configuration files saved under config/autoload are all merged into one. On a live system, this process is unnecessary because it is very rare that the config changes during the life cycle of a request.
Enabling the cache listener
To enable configuration caching as well generate a map for your modules simply open config/application.config.php and add lines 18, 20, 22, 24 and 26 under the 'module_listener_options' key.
return array( 'modules' => array( 'Blog', ), 'module_listener_options' => array( 'module_paths' => array( './module', './vendor', ), 'config_glob_paths' => array( 'config/autoload/{,*.}{global,local}.php', ), 'config_cache_enabled' => true, 'config_cache_key' => 'config-cache', 'module_map_cache_enabled' => true, 'module_map_cache_key' => 'module-map', 'cache_dir' => 'data/cache/module', ), );
With your new settings in place, give your application a couple of loads in your browser. If all is well, the Zend application should have merged all relevant configuration and saved it into its own file for quicker lookups on future requests. You should also see another file containing your module maps.
What is a module map?
A module map is an array containing key-value pairs of every module's file system path to its Module.php class. This class is used to instantiate and load the module. Here is one for my Blog module.
return array ( 'Blog\\Module' => '/var/www/zf2/module/Blog/Module.php', );
The point of this map is to take some of the burden away from the class autoloader by providing a complete path to the class for instantiation.
Wrapping up
If you open your newly created module-config-cache.config-cache.php file, you will see the routes, services and view manager templates from all of your module configs merged under their respective array keys.
Beware!
With config caching now enabled, any further changes to your module's module.config.php files will be ignored as the application will simply refer to the static configuration that is cached. If you are to add new routes and controllers etc, you can either delete the two cached files under data/cache/module or, simply change the 'config_cache_key' and 'module_map_cache_key' values in your application.config.php file each time you need to refresh your config.
Thanks for taking the time to read this tutorial. You can read the next post on ZF2 caching here.