Zend Framework 2 : Cache Module Config and Module Classmap
We already know, that all module configs and class maps over modules are merged. But merging in every request is not a good idea because it will pay the time. If our application is run on production environment, our ZF2 application should use cache to cache config and the classmap too (Whether or not you create a class map for your modules classes, class map cache will be created automatically.). So, it can reduce time access, and of course, reduce memory to process it in the next time refresh.
I will show you how to use it.
1. Create a cache directory like the following

2. Make the cache directory writable
sudo chmod -R 777 /path/to/zf21_learn/data/cache/modulecache
3. Configure config/application.config.php
return array(
'modules' => array(
'Application',
'ZFTool',
'Album'
'ZendDeveloperTools'
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor'
),
'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php'),
// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
'config_cache_enabled' => true,
// The key used to create the configuration cache file name.
'config_cache_key' => "2245023265ae4cf87d02c8b6ba991139",
// Whether or not to enable a module class map cache.
// If enabled, creates a module class map cache which will be used
// by in future requests, to reduce the autoloading process.
'module_map_cache_enabled' => true,
// The key used to create the class map cache file name.
'module_map_cache_key' => "496fe9daf9baed5ab03314f04518b928",
// The path in which to cache merged configuration.
'cache_dir' => "./data/cache/modulecache",
)
);
4. Test run your ZF2 App
Once you’ve run, your cache files will be created like the following :

References :
1. https://github.com/zendframework/ZendSkeletonApplication/blob/master/config/application.config.php
Congratulations for all your input, I am following all your blog, from Colombia.
Thanks.
Hi samsonasik,
In this post, module config and module classmap cached in file. I wanna cache it in RAM (APC or Memcached), would you like write another post about caching in RAM. Thanks a lot your post.
I’m following your blog, I from Vietnamese
i’m not familiar yet about apc, thanks for the suggest.
Hey samsonasik,
i tried the module and classmap cache, but Application crashed during usage of your Zend\Cache\Storage\Filesystem cache configuration together with module cache. Here is the article i mean : http://samsonasik.wordpress.com/2012/09/27/zend-framework-2-using-zendcache-and-hydratingresultset-to-save-database-resultset/
I think there is a missconfiguration in the global.php configuration code. But i am not sure. Thank a lot for checking. I tested some different possibilities of configurations, but nothing worked fine.
The Error Message i got is :
Call to undefined method Closure::__set_state() in D:\Projects\…\data\cache\modulecache\module-config-cache.2245023265ae4cf87d02c8b6ba991139.php on line 68
In module-config-cache.2245023265ae4cf87d02c8b6ba991139.php:
‘service_manager’ =>
array (
‘factories’ =>
array (
…
‘Zend\\Cache\\Storage\\Filesystem’ =>
Closure::__set_state(array(
)),
),
…
Hope this helps to find the problem in configuration
Best regards
mludewig
just work for me, please check php version and zf version you use, i suggest to use latest master.
i used php5.4 .12 and latest stable of ZF2 (2.14) . thats really strange. I build a new Project only with filesystemcache and module.config.cache, but same results. It seems that the Filesystem cache is not complete configured, because there is a Closeure::_set_state() in module.config.cache.php File after generate. What means that Closure Entry ??
Best regards
mludewig
it’s weird. try set config_cache_enabled and module_map_cache_enabled to false first. if problem persist, i think you should submit an issue to zf2 : https://github.com/zendframework/zf2/issues/
Found a working solution
Removed code from global.php and added cache config to Application/Module.php. works fine now. thanks a lot.
best regards
mludewig
you’re welcome
If i add this piece of code to my application.config (or module.config) it doesn’t add files to the caching directory. Do you need to enable filesystem caching first?
nevermind, i misplaced the piece of configuration code!
ok