使 Zend-Framework 运行得更快

发布于 2024-09-29 08:15:01 字数 146 浏览 8 评论 0原文

除了 Zend Optimizer 之外,还有哪些使 Zend-Framwork 运行得更快的最佳方法?

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

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

What are the best ways to make Zend-Framwork run faster besides Zend Optimizer?

If I remember correctly, parsing .ini files in PHP takes a long time. Therefor I cache it (the file won't change during a request)

Are there any other ways to improve ZF's performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

洛阳烟雨空心柳 2024-10-06 08:15:01

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

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

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

<?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根目录中)更改为:

<?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文件。祝你好运。

I cache my application.ini Like this:

Make sure you have the following directory (your cache dir): /application/data/cache

I extend Zend_Application with My_Application, see code:

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

And I change my index.php (in the public root) to:

<?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();

Reload the page and you see the ini file cached. Good luck.

橘味果▽酱 2024-10-06 08:15:01

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

采埃孚发布了一份很好的优化指南:
http://framework.zend.com/manual/en/performance.classloading.html

简而言之,

  1. 在重要的地方使用缓存:数据库查询/复杂操作、整页缓存等。
  2. 根据文档剥离 require_once 调用以支持自动加载。
  3. 缓存 PluginLoader 文件/类映射

如果您想了解更多,

  1. 请跳过使用 Zend_Application 组件
  2. 启用某种操作码缓存
  3. 执行其他典型的 PHP 优化方法(分析、内存缓存等)

Parsing .ini files may be a little slow, but I wouldn't expect that to be anywhere near the slowest part of a typical ZF application. Without seeing any results, it seems like including a bunch of files (Zend_Cache_*) may in some cases be even slower than parsing a simple .ini file. Anyway, that's just one area...

ZF has published a good guide on optimization:
http://framework.zend.com/manual/en/performance.classloading.html

In short,

  1. Utilize caching where it matters: database queries / complex operations, full page cache, etc.
  2. Strip require_once calls in favor of auto-loading as per the documentation.
  3. Cache PluginLoader file/class map

If you want to get a bit more into it,

  1. Skip using Zend_Application component
  2. Enable some sort of op-code cache
  3. Do other typical PHP optimization methods (profiling, memory caching, etc.)
坐在坟头思考人生 2024-10-06 08:15:01

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

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

查看此链接:

http://devblog.policystat。 com/php-to-django-changing-the-engine-while-the-c

Why did you deleted your last question?
I had a good link for you:

I heard things like this before, but
the combination is often related to
migration from one platform to other.

Check at this link:

http://devblog.policystat.com/php-to-django-changing-the-engine-while-the-c

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文