Zend配置Ini缓存


Zend Config Ini Caching

我的Zend应用程序使用3个ini配置文件,总共有200多行要解析,包含100多个指令。每次请求都解析这些文件吗?有些人说他们是(像这里和这里)。如果是这样,这不是一个效率问题吗?

这些链接里的评论褒贬不一——有些人说你应该避免ini配置文件,用PHP做配置,有些人说你可以用Zend_Cache_Frontend_File,还有一些人说这根本不是问题。但是,如果您期望有相当大的流量,那么为每个请求解析200行文本肯定会很快成为一个问题。

如果你推荐使用缓存技术,你能解释一下你将如何实现它吗?

是的,它们每次都被解析,除非你缓存它们。它确实节省了时间(我在我自己的项目中检查过)。

那么如何使用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

如果您加载像ZF这样的框架,您将加载包含数千行代码的数十个文件。必须为每个用户和请求读取这些文件。在服务器端,你有一些缓存磁盘控制器和诸如此类的东西,这样文件就不必每次都从磁盘读取了。此外,操作系统中的内存管理器会跟踪它,并为内存提供一些缓存,这样就不必每次都将其读入内存,只需将其取出。接下来,您通常有一个数据库,其中或多或少会发生相同的事情,因为数据库最终存储在硬盘驱动器上的文件中。DB服务器读取文件和相同的故事或多或少。

那么,我会担心配置文件中的几行吗?当然不是,因为我需要数据,我的应用程序工作。它来自哪里是次要的。

关于Zend_Cache缓存。如果你的数据很紧凑,不需要像Zend_Config中的文件那样进行大量处理,你将节省几微秒的时间。你实际上是将一种压缩格式存储到另一种压缩格式中;必须再次反序列化的序列化字符串。如果你可以缓存数据来避免数据库访问,或者渲染整个视图,包括所有的数据请求和模型,我们谈论的是一个完全不同的故事。

如果你假设PHP文件缓存在内存中,而解析后的ini文件也在内存中,你可以在跳过Zend_Config_Ini类和解析过程时将ini文件转换为PHP文件。

# public/index.php
$cachedConfigFile = APPLICATION_PATH.'/../cache/application.ini.'.APPLICATION_ENV.'.php';
if(!file_exists($cachedConfigFile) || filemtime($cachedConfigFile) < filemtime(APPLICATION_PATH . '/configs/application.ini'))
{
    require_once 'Zend/Config/Ini.php';
    $config = new Zend_Config_Ini( APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV );
    file_put_contents($cachedConfigFile, '<?php '.PHP_EOL.'return '.var_export($config->toArray(),true).';' );
}
$application = new Zend_Application(
    APPLICATION_ENV,
    $cachedConfigFile // originally was APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
        ->run();

我已经测试了ab.非缓存配置:

ab -n 100 -c 100 -t 10 http://localhost/
Requests per second:    45.57 [#/sec] (mean)
Time per request:       2194.574 [ms] (mean)
Time per request:       21.946 [ms] (mean, across all concurrent requests)
Transfer rate:          3374.08 [Kbytes/sec] received

vs缓存配置:

Requests per second:    55.24 [#/sec] (mean)
time per request:       1810.245 [ms] (mean)
Time per request:       18.102 [ms] (mean, across all concurrent requests)
Transfer rate:          4036.00 [Kbytes/sec] received

性能提高18%。

根据创建文件的朋友:

https://github.com/QualityCase/Mini-Case/blob/master/modules/admin/Bootstrap.php

我发现这很有用:

protected function _initConfig()
{
    if(!$config = $this->_cache->load('config')) {
        $config = new Zend_Config_Ini(BASE_PATH . '/configs/application.ini');
        $config = $config->toArray();
        $this->_cache->save($config, 'config');
    }
    Zend_Registry::set('config', $config);
}