代码之家  ›  专栏  ›  技术社区  ›  Wouter Dorgelo voy

使Zend框架运行得更快

  •  6
  • Wouter Dorgelo voy  · 技术社区  · 14 年前

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

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

    4 回复  |  直到 13 年前
        1
  •  8
  •   Wouter Dorgelo voy    14 年前

    我将application.ini缓存如下:

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

    我扩展 Zend_Application My_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;
        }
    }
    

    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
  •  5
  •   Adrian Schneider    14 年前

    ZF出版了一本关于优化的好指南: http://framework.zend.com/manual/en/performance.classloading.html

    简而言之,

    1. 在重要的地方使用缓存:数据库查询/复杂操作、全页缓存等。
    2. 缓存插件加载程序文件/类映射

    如果你想再深入一点,

    1. 启用某种操作代码缓存
    2. 执行其他典型的PHP优化方法(分析、内存缓存等)
        4
  •  0
  •   Karma police    14 年前

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

    这种组合通常与 从一个平台迁移到另一个平台。

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