是的,他們每次都被解析,除非你緩存它們。這確實節省了時間(我在自己的項目中檢查過)。
那麼你如何使用Zend_Cache_Frontend_File
緩存一個ini文件呢?那麼我可以爲你提供一個例子。在項目中,我有一個包含了許多定製路線route.ini文件:
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
在我bootstrap.php中我使用高速緩存(如果可能的話)加載它們:
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');
}
緩存是建立在我的的application.ini如下
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
希望這會有所幫助。
馬辛,您的文章幫了我,現在我已經用它更好的性能。但是怎麼樣,而不是試圖加載緩存對象: $ cache = $ cacheManager-> getCache('configFiles'); ($ cacheManager-> hasCache($ routeCache)){ $ cache = $ cacheManager-> getCache($ routeCache);如果它首先存在,則執行額外檢查: if // ... } –
爲我工作。謝謝。 –