Modern new features of PHP series (six) - Zend Opcache

1 Overview

     New features PHP bytecode cache is not, there are many separate extension can be achieved, such as APC, eAccelerator and Xache, etc., but as of now these extensions are not integrated into the PHP core, starting from PHP 5.5.0, PHP built-in bytes code caching feature called Zend Opcache.

     Before we begin, let's take a look at what bytecode cache, and what the role of the bytecode cache Yes.

     As we all know, PHP is an interpreted language, the virtual machine built on Zend, PHP interpreter when executing a PHP script parses the PHP script code, the code is compiled into a series of Zend PHP opcode (opcode: http://php.net/ Manual / ZH / internals2.opcodes.php , since each opcode is one byte long, so called bytecode, the bytecode can be directly executed Zend virtual machine), and then execute the bytecode. PHP file for each request like this, it will consume a lot of resources, if every HTTP request must continue to parse, compile and execute PHP scripts, consume more resources. If the PHP source code unchanged, the corresponding byte code is not changed, it is clear there is no need to regenerate each time Opcode, combined with the ubiquitous Web application caching mechanism, we can put the first generation of Opcode cached, so the next times taken directly from the cache, is it soon? Below is a flow chart Opcode caching enabled before and after:

 

       When the bytecode cache can store a precompiled PHP bytecode, so that the next request PHP script, PHP interpreter do not always read, parsed and compiled PHP code, pre-compiled word read directly from memory section code, and then executed immediately, this can save a lot of time, greatly enhance the performance of applications.

2, enable Zend Opcache

NOTE: If you are using Windows development environment, or use brewor apt-getcommands such as installation of PHP can skip the compilation step.

       By default, Zend Opcache not open, we need to use at compile time --enable-opcachespecified to enable Zend Opcache.

After compiled PHP also need to php.inispecify the Opcache expansion path:

    zend_extension=/path/to/opcache.so

       In general PHP Zend Opcache expansion path will be displayed after the compilation is successful, but if you can not remember, you can use the following command to find the directory where the PHP extensions:

    php-config --extension-dir

Note: If you use Xdebug, need php.inito load Zend Opcache, and then load Xdebug.

       Update php.iniafter the restart and see if the PHP process to enable successful:

      

3, configure Zend Opcache

        Enabled Zend Opcache also need php.iniconfiguration Zend Opcache, the following is an example of a configuration as a reference:

    opcache.validate_timestamps=1             //生产环境中配置为0
    opcache.revalidate_freq=0                 //检查脚本时间戳是否有更新时间
    opcache.memory_consumption=64             //Opcache的共享内存大小,以M为单位
    opcache.interned_strings_buffer=16        //用来存储临时字符串的内存大小,以M为单位
    opcache.max_accelerated_files=4000        //Opcache哈希表可以存储的脚本文件数量上限
    opcache.fast_shutdown=1                   //使用快速停止续发事件

注:后续我们还会进一步介绍Zend Opcache的配置,PHP官网中列出了Zend Opcache的全部设置:http://ua2.php.net/manual/zh/opcache.configuration.php

4、使用Zend Opcache

         Zend Opcache使用起来很简单,因为启用之后它会自动运行,Zend Opcache会自动在内存中缓存预先编译好的PHP字节码,如果缓存了某个文件的字节码,就执行对应的字节码。

         如果php.ini中配置了opcache.validate_timestamps值为0,需要小心,因为Zend Opcache将不能觉察PHP脚本的变化,必须手动清空Zend OPcache缓存的字节码,才能让它发现PHP文件的变动。这个配置适合在生产环境中设置为0,但在开发环境会带来不便,我们可以在开发环境中这样配置启用自动验证缓存功能:

    opcache.validate_timestamps=1
    opcache.revalidate_freq=0

 

Guess you like

Origin www.cnblogs.com/mzhaox/p/11222026.html