2010-10-25 91 views
6

除Zend Optimizer之外,讓Zend-Framwork運行更快的最佳方式是什麼?讓Zend-Framework運行更快

如果我沒有記錯,在PHP中解析.ini文件需要很長時間。因此,我緩存它(文件在請求期間不會改變)

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

回答

8

我緩存我的application.ini像這樣:

請確保您有以下目錄(緩存目錄):/application/data/cache

我延長Zend_ApplicationMy_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文件。祝你好運。

+2

是什麼時候的application.ini緩存的性能提升?你有這個基準嗎? – 2010-10-26 08:27:43

+0

這取決於你的ini文件的大小。將它看作是緩存你的CSS文件並加載或不加載緩存。 – 2010-11-19 05:01:51

+0

@Jurian 2-5%在我的(ini-heavy)應用程序中。 – ReactiveRaven 2011-12-13 15:11:43

5

解析.ini文件可能會有點慢,但我不會期望這是近一個典型的ZF應用最慢的部分的任何地方。沒有看到任何結果,似乎包括一堆文件(Zend_Cache_ *)在某些情況下可能比解析一個簡單的.ini文件更慢。無論如何,這是隻有一個區域......

ZF發表了關於優化的一個很好的指南: http://framework.zend.com/manual/en/performance.classloading.html

總之,

  1. 利用高速緩存的地方事務:數據庫查詢/複雜的操作,全頁面緩存等。
  2. 根據文檔條帶require_once調用自動加載。
  3. 緩存插件加載文件/類映射

如果你想獲得多一點進去,用Zend_Application組件

  • 啓用某種運算代碼緩存的

    1. 跳過
    2. 其他典型的PHP優化方法(分析,內存緩存等)
  • +0

    一旦Zend_Cache_ *在操作碼緩存中,它們就不再緩慢。如果你不緩存解析的INI文件,它將一直很慢。在我的應用程序中,在開始優化之前,解析INI文件是請求的絕大多數(75-90%)。非常容易配置,對速度沒有那麼熱。 – ReactiveRaven 2011-12-13 15:08:41

    +0

    75-90%的請求?你很認真嗎? – Maxence 2012-05-02 08:54:28

    +0

    對不起,應該閱讀引導過程。 70%是整個Zend_Application/bootstrap過程。 Zend_Config其實很慢,但絕對不是70%。 – 2012-05-02 18:16:14