Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Cache Module Config and Module Classmap

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on March 13, 2013

zf2-zendframework2We 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
Screen Shot 2013-03-13 at 2.50.25 AM
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(
    'module_listener_options' => array(
        'module_paths' => array(
        '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 :
Screen Shot 2013-03-13 at 2.57.51 AM

References :
1. https://github.com/zendframework/ZendSkeletonApplication/blob/master/config/application.config.php

31 Responses

Subscribe to comments with RSS.

  1. Ernesto Quintero said, on March 13, 2013 at 6:38 am

    Congratulations for all your input, I am following all your blog, from Colombia.

  2. tmquang said, on March 16, 2013 at 6:24 pm

    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

    • samsonasik said, on March 17, 2013 at 1:11 am

      i’m not familiar yet about apc, thanks for the suggest.

    • Cà Rốt said, on July 7, 2014 at 4:45 pm

      do you config cache in RAM? you supports for me! thanks you

  3. mludewig said, on March 22, 2013 at 4:19 pm

    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’ =>

    Hope this helps to find the problem in configuration🙂

    Best regards

    • samsonasik said, on March 23, 2013 at 4:54 am

      just work for me, please check php version and zf version you use, i suggest to use latest master.

      • mludewig said, on March 25, 2013 at 2:47 pm

        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

      • samsonasik said, on March 25, 2013 at 7:43 pm

        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/

    • conceptdeluxe said, on June 8, 2013 at 1:04 pm

      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.

    • M.R.Smit said, on June 27, 2013 at 2:38 am

      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!)

  4. mludewig said, on March 25, 2013 at 9:55 pm

    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

  5. Jeroen said, on April 4, 2013 at 2:26 pm

    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?

    • Jeroen said, on April 4, 2013 at 8:05 pm

      nevermind, i misplaced the piece of configuration code!

  6. Michael said, on June 13, 2013 at 6:36 pm

    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!

  7. Kasper Svendsen said, on June 16, 2014 at 4:08 pm

    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?

    • samsonasik said, on June 17, 2014 at 1:40 am

      no, you can comment the cache config code, refresh, and re-activate the cache again.

  8. may saghira said, on October 14, 2014 at 9:34 pm

    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

  9. Harshal said, on February 4, 2015 at 3:04 pm

    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?

    • samsonasik said, on February 4, 2015 at 4:21 pm

      i’ve no idea about that, it should be tricky, override existing functionality.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: