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(
        '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 :
Screen Shot 2013-03-13 at 2.57.51 AM

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

About these ads

13 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.

  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 : 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

    • 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
        mludewig

      • 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/

  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
    mludewig

  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!


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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 102 other followers

%d bloggers like this: