New features of modern PHP series (6) -- ZendOpcache

Source: Internet
Author: User
New features of modern PHP series (6) -- ZendOpcache 1. Overview

Bytecode cache is not a new feature of PHP, and many independent extensions can be implemented, such as APC, eAccelerator, and Xache. Up to now, these extensions have not been integrated into the PHP kernel, starting from PHP 5.5.0, PHP has a built-in bytecode cache function named Zend Opcache.

Before starting, let's take a look at what is the bytecode cache and what is the role of the bytecode cache.

As we all know, PHP is an interpreted language built on the Zend virtual machine. when executing the PHP script, the PHP interpreter parses the PHP script code and compiles the PHP code into a series of Zend operation codes (opcode: http://php.net/manual/zh/internals2.opcodes.php, because each operation code is a byte long, so also called bytecode, bytecode can be directly executed by the Zend virtual machine), and then execute bytecode. This is the case for every request to the PHP file, which consumes a lot of resources. if each HTTP request must constantly parse, compile, and execute PHP scripts, more resources will be consumed. If the PHP source code remains unchanged, the corresponding bytecode will not change. Obviously, there is no need to re-generate Opcode every time. in combination with the ubiquitous Cache mechanism in Web applications, we can cache the first generated Opcode so that we can directly retrieve it from the cache next time. isn't it fast? The following flowchart shows the process before and after Opcode caching is enabled:

The bytecode cache can store pre-compiled PHP bytecode. in this way, the PHP interpreter does not need to read, parse, and compile PHP code each time when a PHP script is requested, read pre-compiled bytecode directly from the memory and execute it immediately. this saves a lot of time and greatly improves application performance.

2. enable Zend Opcache

Note: If you are using a Windows development environment or using commands such as brew or apt-get to install PHP, you can skip the compilation steps.

By default, Zend Opcache is not enabled. you need to use -- enable-opcache to enable Zend Opcache during compilation.

After compiling PHP, you also need to specify the Opcache extension path in php. ini:

zend_extension=/path/to/opcache.so

Generally, the Zend Opcache extension path is displayed after PHP compilation is successful. if you cannot find the extension path, run the following command to find the Directory of the PHP extension:

php-config --extension-dir

Note: If you use Xdebug, you need to first load Zend Opcache in php. ini and then load Xdebug.

After updating php. ini, restart the PHP process and check whether it is enabled successfully:

3. configure Zend Opcache

After Zend Opcache is enabled, you also need to configure Zend Opcache in php. ini. The following is a configuration example for reference:

Opcache. validate_timestamps = 1 // Set it to 0opcache in the production environment. revalidate_freq = 0 // check whether the script timestamp has the update time opcache. memory_consumption = 64 // Opcache memory size, in MB. interned_strings_buffer = 16 // used to store the memory size of the temporary string, in MB as the unit of opcache. max_accelerated_files = 4000 // the maximum number of script files that can be stored in the Opcache hash table is opcache. fast_shutdown = 1 // use the Quick Stop event

Note: We will further introduce the configuration of Zend Opcache, PHP official website lists all the Zend Opcache settings: http://ua2.php.net/manual/zh/opcache.configuration.php.

4. use Zend Opcache

Zend Opcache is easy to use because it runs automatically after it is enabled. Zend Opcache automatically caches pre-compiled PHP bytecode in the memory. if the bytecode of a file is cached, the corresponding bytecode is executed.

If php. opcache is configured in ini. the validate_timestamps value is 0. be careful because Zend Opcache cannot detect changes in the PHP script. you must manually clear the bytecode cached by Zend OPcache to detect changes in the PHP file. This configuration is suitable for setting it to 0 in the production environment, but it will cause inconvenience in the development environment. we can configure it in the development environment to enable the automatic verification cache function:

opcache.validate_timestamps=1opcache.revalidate_freq=0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.