Php 让Zend框架运行得更快

Php 让Zend框架运行得更快,php,zend-framework,caching,optimization,zend-optimizer,Php,Zend Framework,Caching,Optimization,Zend Optimizer,除了Zend Optimizer之外,让Zend Framwork运行得更快的最佳方法是什么 如果我没记错的话,用PHP解析.ini文件需要很长时间。因此,我缓存它(在请求期间文件不会更改) 还有其他方法可以提高ZF的性能吗?我会像这样缓存我的application.ini: 确保您有以下目录(缓存目录):/application/data/cache 我使用My_应用程序扩展Zend_应用程序,请参见代码: <?php require_once 'Zend/Application.php

除了Zend Optimizer之外,让Zend Framwork运行得更快的最佳方法是什么

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


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

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

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

我使用
My_应用程序
扩展
Zend_应用程序
,请参见代码:

<?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;
    }
}

重新加载页面,您将看到缓存的ini文件。祝你好运。

另请参见

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

采埃孚出版了一本很好的优化指南:

总之,

  • 在重要的地方利用缓存:数据库查询/复杂操作、整页缓存等
  • 根据文件要求,剥离需要一次调用,以便自动加载
  • 缓存插件加载程序文件/类映射
  • 如果你想更深入一点

  • 跳过使用Zend_应用程序组件
  • 启用某种操作码缓存
  • 执行其他典型的PHP优化方法(评测、内存缓存等)

  • 你为什么删除了你的最后一个问题? 我有一个很好的链接:

    我以前听说过这样的事,但是 这种组合通常与 从一个平台迁移到另一个平台

    查看此链接:


    缓存application.ini时性能的提高是什么?你有这方面的基准吗?这取决于你的ini文件的大小。。将其视为缓存您的CSS文件并加载它(带缓存或不带缓存)。@Jurian 2-5%在我的(ini重)应用程序上。一旦Zend_缓存在操作码缓存中,它们就不再慢了。如果不缓存已解析的INI文件,它将始终很慢。在我的应用程序中,在开始优化之前,解析INI文件是请求的绝大多数(75-90%)。很好的配置,速度不是很快。75-90%的要求?你是认真的吗?抱歉,这应该是引导过程。70%是整个Zend_应用程序/引导过程。Zend_Config实际上相当慢,但绝对不是70%。您答案中的链接现在似乎已失效。我猜是这样的:这与其说是答案,不如说是评论。至少提供一些解释,以防将来链接死亡。
    <?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();