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.
do you config cache in RAM? you supports for me! thanks you
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 : https://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/
You should only use static calls within the configuration – e.g. when using service manager factories that require instantiated classes use getServiceConfig method in module.php to return the factory array instead.
thanks 😉
the solution is simple (yet it took me also a while to figure out).
closures cannot be serialized.
`Zend\\Cache\\Storage\\Filesystem’ => ‘path\\to\valid\\factory\namespace’
in other words. for your production environment, convert all closures to proper factories (it’s also faster!)
+1
Thanks for explanation
+1
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 😀
No matter what kind of problem i have with zf2, the troubleshooting every time is the same:
First look at samsonasik’s blog and if there is no answer (which happens very rarely) look at the official zf2 doku. Thank you so much for sharing your experiences!
you’re welcome ;)… Thank you for visiting 😉
[…] https://samsonasik.wordpress.com/2013/03/13/zend-framework-2-cache-module-config-module-classmap-cach… […]
Thank you samsonasik
Will the cache automatically invalidate on config/module change?
If not – what is your prefered strategy to invalidate cache on software update?
no, you can comment the cache config code, refresh, and re-activate the cache again.
Thanks samsonasik for the tuto , just being worried about the error below :
Fatal error: Call to undefined method Closure::__set_state() in C:\wamp\www\mycoreg\data\cache\modulecache\module-config-cache.2245023265ae4cf87d02c8b6ba991139.php on line 550
you’ve closure somewhere in your module(s). replace them with factory, read this https://samsonasik.wordpress.com/2013/01/02/zend-framework-2-cheat-sheet-service-manager/ at factory’s part.
Ok solve that problem thanks ,may i know what’s the lifetime of those files ? regards , may saghira.
AFAIK, you need to remove it manually for now…
thanks samsonasik.
Is it possible to add some error handling mechanism, that if it cannot allocate memory in server then it will fetch file from the disk else fetch from cache?
i’ve no idea about that, it should be tricky, override existing functionality.