phpzend-frameworkzend-cachezend-config

Zend Config Ini Caching


My Zend app uses 3 ini config files, with a total of over 200 lines to parse, containing over 100 instructions. Are these files parsed every request? Some people say they are (like here and here). If so, is this not an efficiency issue?

The comments in those links have mixed sentiment - some say you should avoid ini config files and do your config in PHP, some say you could use Zend_Cache_Frontend_File, and some say it's just not an issue. But surely parsing 200 lines of text for every request will quickly become a problem if you are expecting quite a lot of traffic?

If you do recommend using a caching technique, can you please explain exactly how you would implement it?


Solution

  • Yes, they are parsed every time unless you cache them. It really saves time (i checked it in my own project).

    So how do you use Zend_Cache_Frontend_File to cache an ini file? Well I can provide you with an example. In my project I have route.ini file that contains a number of custom routes:

    routes.ini

    routes.showacc.route = "/@show/:city/:id/:type"
    routes.showacc.type = "Zend_Controller_Router_Route" 
    routes.showacc.defaults.module = default
    routes.showacc.defaults.controller = accommodation
    routes.showacc.defaults.action = show
    routes.showacc.defaults.city = 
    routes.showacc.defaults.type = 
    routes.showacc.defaults.id = 
    routes.showacc.defaults.title = 
    routes.showacc.reqs.id = "\d+" 
    
    ;and more
    

    In my Bootstrap.php I load them using cache (if possible):

    protected function _initMyRoutes() {
        $this->bootstrap('frontcontroller');
        $front = Zend_Controller_Front::getInstance();
        $router = $front->getRouter();
    
        // get cache for config files
        $cacheManager = $this->bootstrap('cachemanager')->getResource('cachemanager');
        $cache = $cacheManager->getCache('configFiles');
        $cacheId = 'routesini';
    
        // $t1 = microtime(true);
        $myRoutes = $cache->load($cacheId);
    
        if (!$myRoutes) {
            // not in cache or route.ini was modified.
            $myRoutes = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
            $cache->save($myRoutes, $cacheId);
        }
        // $t2 = microtime(true);
        // echo ($t2-$t1); // just to check what is time for cache vs no-cache scenerio
    
        $router->addConfig($myRoutes, 'routes');
    }
    

    The cache is setup in my application.ini as follows

    resources.cachemanager.configFiles.frontend.name = File
    resources.cachemanager.configFiles.frontend.customFrontendNaming = false
    resources.cachemanager.configFiles.frontend.options.lifetime = false
    resources.cachemanager.configFiles.frontend.options.automatic_serialization = true
    resources.cachemanager.configFiles.frontend.options.master_files[] = APPLICATION_PATH "/configs/routes.ini"    
    resources.cachemanager.configFiles.backend.name = File
    resources.cachemanager.configFiles.backend.customBackendNaming = false
    resources.cachemanager.configFiles.backend.options.cache_dir = APPLICATION_PATH "/../cache"
    resources.cachemanager.configFiles.frontendBackendAutoload = false
    

    Hope this helps.