Modern PHP new features series (vi)--Zend Opcache

Source: Internet
Author: User
Tags php source code

1. Overview

Bytecode caching is not a new feature of PHP, there are a lot of independent extensions can be implemented, such as APC, Eaccelerator and Xache, but up to now these extensions are not integrated into the PHP kernel, php 5.5.0 starting from PHP built-in byte code cache function, called Zend Opcache.

Before we get started, let's look at what byte-code caching is and what the bytecode cache does.

As we all know, PHP is an interpreted language, built on the Zend virtual machine, PHP interpreter in the execution of PHP script will parse the PHP script code, the PHP code compiled into a series of Zend opcode (opcode:http://php.net/manual/zh/ internals2.opcodes.php, because each opcode is a byte long, so called bytecode, bytecode can be directly Zend virtual machine execution), and then execute bytecode. Every time a PHP file is requested, this consumes a lot of resources, and if each HTTP request must constantly parse, compile, and execute PHP scripts, it consumes more resources. If the PHP source code is not changed, the corresponding bytecode will not change, obviously there is no need to regenerate opcode every time, combined with the ubiquitous caching mechanism in Web applications, we can cache the first generation of opcode, so the next time directly from the cache, it is not very fast? Here is the flowchart before and after enabling the opcode cache:

Bytecode caching can store precompiled PHP bytecode so that the next time PHP script is requested, the PHP interpreter does not have to read, parse, and compile PHP code every time, read precompiled bytecode directly from memory, and then execute immediately, which saves a lot of time and greatly improves the performance of the application.

2. Enable Zend Opcache

Note: If you use a Windows development environment, or if you install PHP using commands such as brew or apt-get, you can skip the compile step.

By default, Zend Opcache is not turned on and requires that we use--enable-opcache to specify enable Zend Opcache at compile time.

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

Zend_extension=/path/to/opcache.so

In general, PHP will display the Zend Opcache extension path after successful compilation, but if you can't remember, you can find the directory where the PHP extension is located using the following command:

Php-config--extension-dir

Note: If you are using Xdebug, you need to load Zend Opcache in php.ini before loading xdebug.

Restart the PHP process after updating php.ini and see if success is enabled:

3, configuration Zend Opcache

After you enable Zend Opcache, you also need to configure Zend Opcache in PHP.ini, which is a sample configuration example as a reference:

Opcache.validate_timestamps=1    //The production environment is configured to 0opcache.revalidate_freq=0    //Check if the script timestamp has an update time Opcache.memory_ Consumption=64    the shared memory size of//opcache, in M opcache.interned_strings_buffer=16    //memory size used to store temporary strings, The    maximum number of script files that can be stored in M opcache.max_accelerated_files=4000//opcache hash table opcache.fast_shutdown=1         //Use fast stop to renew events

Note: We will further introduce the configuration of Zend Opcache in the following section, and the 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 simple to use because it automatically runs after it is enabled, Zend Opcache automatically caches pre-compiled PHP bytecode in memory, and executes the corresponding bytecode if the byte code of a file is cached.

If PHP.ini is configured with a opcache.validate_timestamps value of 0, be careful, because Zend Opcache will not be aware of changes in PHP scripts, you must manually empty the Zend Opcache cached bytecode, To let it discover the changes in PHP files. This configuration is intended to be set to 0 in a production environment, but in a development environment, we can configure the Enable automatic validation caching feature in the development environment:

Opcache.validate_timestamps=1opcache.revalidate_freq=0
  • Related Article

    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.