【问题标题】:Making Zend-Framework run faster让 Zend-Framework 运行得更快
【发布时间】:2011-04-30 07:29:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: php zend-framework caching optimization zend-optimizer


    【解决方案1】:

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

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

    查看此链接:

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

    【讨论】:

      【解决方案2】:

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

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

      总之,

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

      如果你想更深入地了解它,

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

      【讨论】:

      • 一旦 Zend_Cache_* 在操作码缓存中,它们就不再慢了。如果不缓存解析过的INI文件,总是会很慢。在我的应用程序中,在我们开始优化之前,解析 INI 文件是请求的绝大部分(75-90%)。非常易于配置,速度不那么热。
      • 您请求的 75-90% ?你是认真的吗?
      • 抱歉,应该阅读引导过程。 70% 是整个 Zend_Application / bootstrap 过程。 Zend_Config 实际上很慢,但绝对不是 70%。
      • 您答案中的链接现在似乎已失效。我猜是这样的:framework.zend.com/manual/1.12/en/performance.classloading.html
      【解决方案3】:

      【讨论】:

      • 这更多的是评论而不是答案。至少提供一些解释,以防将来链接失效。
      【解决方案4】:

      我像这样缓存我的 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 文件。祝你好运。

      【讨论】:

      • 缓存application.ini后性能提升多少?你有这方面的基准吗?
      • 这取决于您的 ini 文件的大小。将其视为缓存您的 CSS 文件并在使用或不使用缓存的情况下加载它。
      • @Jurian 2-5% 在我的(ini-heavy)应用程序上。
      猜你喜欢
      • 2011-05-10
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 2010-11-06
      • 2023-03-26
      • 1970-01-01
      • 2017-07-10
      相关资源
      最近更新 更多