首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让Zend-Framework运行得更快

让Zend-Framework运行得更快
EN

Stack Overflow用户
提问于 2010-10-26 00:05:15
回答 4查看 4.9K关注 0票数 6

除了Zend Optimizer之外,还有什么最好的方法让Zend-Framwork运行得更快?

如果我没记错的话,在PHP中解析.ini文件需要很长时间。因此,我将其缓存(该文件在请求期间不会更改)

有没有其他方法可以提高ZF的性能?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-10-26 00:27:41

我像这样缓存我的application.ini:

确保您有以下目录(缓存目录):/application/data/cache

我用My_Application扩展了Zend_Application,参见代码:

代码语言:javascript
复制
<?php
require_once 'Zend/Application.php';

class My_Application extends Zend_Application
{

    /**
     * Flag used when determining if we should cache our configuration.
     */
    protected $_cacheConfig = false;

    /**
     * Our default options which will use File caching
     */
    protected $_cacheOptions = array(
        'frontendType' => 'File',
        'backendType' => 'File',
        'frontendOptions' => array(),
        'backendOptions' => array()
    );

    /**
     * Constructor
     *
     * Initialize application. Potentially initializes include_paths, PHP
     * settings, and bootstrap class.
     *
     * When $options is an array with a key of configFile, this will tell the
     * class to cache the configuration using the default options or cacheOptions
     * passed in.
     *
     * @param  string                   $environment
     * @param  string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
     * @throws Zend_Application_Exception When invalid options are provided
     * @return void
     */
    public function __construct($environment, $options = null)
    {
        if (is_array($options) && isset($options['configFile'])) {
            $this->_cacheConfig = true;

            // First, let's check to see if there are any cache options
            if (isset($options['cacheOptions']))
                $this->_cacheOptions =
                    array_merge($this->_cacheOptions, $options['cacheOptions']);

            $options = $options['configFile'];
        }
        parent::__construct($environment, $options);
    }

    /**
     * Load configuration file of options.
     *
     * Optionally will cache the configuration.
     *
     * @param  string $file
     * @throws Zend_Application_Exception When invalid configuration file is provided
     * @return array
     */
    protected function _loadConfig($file)
    {
        if (!$this->_cacheConfig)
            return parent::_loadConfig($file);

        require_once 'Zend/Cache.php';
        $cache = Zend_Cache::factory(
            $this->_cacheOptions['frontendType'],
            $this->_cacheOptions['backendType'],
            array_merge(array( // Frontend Default Options
                'master_file' => $file,
                'automatic_serialization' => true
            ), $this->_cacheOptions['frontendOptions']),
            array_merge(array( // Backend Default Options
                'cache_dir' => APPLICATION_PATH . '/data/cache'
            ), $this->_cacheOptions['backendOptions'])
        );

        $config = $cache->load('Zend_Application_Config');
        if (!$config) {
            $config = parent::_loadConfig($file);
            $cache->save($config, 'Zend_Application_Config');
        }

        return $config;
    }
}

并且我将我的index.php (在public根目录中)更改为:

代码语言:javascript
复制
<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** My_Application */
require_once 'My/Application.php';

// Create application, bootstrap, and run
$application = new My_Application(
    APPLICATION_ENV,
    array(
            'configFile' => APPLICATION_PATH . '/configs/application.ini'
    )
);
$application->bootstrap()
            ->run();

重新加载页面,您会看到ini文件被缓存。祝好运。

票数 8
EN

Stack Overflow用户

发布于 2010-11-18 01:26:28

解析.ini文件可能有点慢,但我不认为这是典型ZF应用程序中最慢的部分。在看不到任何结果的情况下,似乎包含一堆文件(Zend_Cache_*)在某些情况下甚至比解析一个简单的.ini文件还要慢。不管怎么说,这只是一个领域...

ZF发布了一个很好的优化指南:http://framework.zend.com/manual/en/performance.classloading.html

总之,

  1. 在关键位置利用缓存:数据库查询/复杂操作、完整页面缓存等。
  2. require_once支持根据documentation.
  3. Cache PluginLoader文件/类映射

进行自动加载

如果你想了解更多,

使用component

  • Enable跳过
  1. 某种类型的操作代码缓存
  2. 执行其他典型的Zend_Application优化方法(性能分析、内存缓存等)
票数 5
EN

Stack Overflow用户

发布于 2010-11-17 15:45:22

另请参阅http://www.kimbs.cn/2009/06/caching-application-ini-for-zend-framework-apps/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4016497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档