phpzend-framework2zend-framework-modules

zend framework 2 - Cache config files


i'm trying to enable the cache for the config files in zend framework 2 :

the module.config.php ( part of services ) :

 'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
            'doctrine.cache.mycache' => function ($sm) {
                 $cache = new \Doctrine\Common\Cache\MemcacheCache();
                     $memcache = new \Memcache();
                     $memcache->connect('localhost', 11211);
                     $cache->setMemcache($memcache);
                 return $cache;
         },
        ),
    ),

the application.config.php ( part of enabling the cache for config ):

'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' => md5('config'),
        'module_map_cache_enabled' => true,
        'module_map_cache_key' => md5('module_map'),
        'cache_dir' => "./data/cache/modulecache",
    ),

And here the error i got :

 Fatal error: Call to undefined method Closure::__set_state()

Thanks.


Solution

  • Config files can't be cached if they contain anonymous functions (in your case, the value for doctrine.cache.mycache). You will need to move just that part out of the config file and into your Module.php class' getServiceConfig() instead. That should fix the issue.