[Modern PHP] Chapter II new features of the six Zend Opcache

Source: Internet
Author: User
Tags configuration settings server hosting
Zend Opcache


Byte-code caching is not new for PHP. We have alternative PHP Cache (APC), Eaccelerator, Ioncube, and XCache as a standalone extension, all of which can be used as our options. However, they are not available in each of the core release versions of PHP. Until now PHP 5.5.0, PHP has its own built-in bytecode cache: Zend Opcache.


First, let me explain what the bytecode is and how important it is. PHP is an explanatory language. When the PHP interpreter executes a PHP script, the interpreter parses the PHP script code, compiles the PHP code into a set of Zend opcodes (machine code instructions), and eventually executes the bytecode. The PHP file repeats the above steps on each request. This is a waste of time, especially when each HTTP request is parsed, compiled, and executed again and again by the PHP script. If we have a way to cache these compiled bytecode, we can shorten the response time of the application and reduce the pressure on the system resources. You're so lucky.


The bytecode cache can store compiled PHP bytecode. This means that the PHP interpreter no longer needs to read, parse, and compile PHP code on each request, but can read the compiled bytecode directly from memory and execute. This saves time and greatly improves the performance of the application.


Open Zend Opcache


Zend Opcache is not enabled by default, you need to explicitly turn on Zend opcache when installing PHP.


If you are using a virtual host, make sure you choose a good service provider that can provide PHP 5.5.0 and above and open Zend Opcache.


If you compile PHP yourself (assuming you are using a VPS or server hosting), you must add a parameter after the./configure command in PHP.

--enable-opcache

After the PHP compilation is complete, you also need to specify the path to the Zend Opcache extension in the Phpini file, referring to the following example:

Zend_extension=/path/to/opcache.so


The file path of the Zend Opcache extension is displayed immediately after the PHP compilation is successful. If you forget to do this as I said, you can also execute the following command to get the path address for all extensions in PHP:

Php-config--extension-dir


If you are using the unmatched Derick Rethans to develop popular debugging tools Xdebug, in php.ini files, the Zend opcache extension must be loaded before the xdebug extension.


After you have updated the php.ini file and restarted the PHP process, you are ready to use it. If you need to confirm that Zend Opcache is installed correctly, you can create a PHP file that contains the following content:

   
View the PHP file in the browser, and drop the scroll bar until you see the Zend Opcache extension, as shown in 2-2. If you do not see this message, it means that Zend Opcache is not running.


Figure 2-2 Zend opcache INI Settings


Configuring Zend Opcache


In the case of Zend Opcache enabled, you can configure Zend Opcache in the php.ini configuration file. Here are the Opcache settings I like to use:

Opcache.validate_timestamps = 1//use "0" in a production environment

Opcache.revalidate_freq = 0

Opcache.memory_consumption = 64

Opcache.interned_strings_buffer = 16

Opcache.max_accelerated_files = 4000

Opcache.fast_shutdown = 1


For more information on these Zend Opcache settings, you can read the eighth chapter. A complete list of settings can be obtained on php.net.


Using Zend Opcache


This section is simple because Zend Opcache will work automatically once it is enabled. Zend Opcache automatically caches the compiled PHP bytecode into memory and automatically executes the stored bytecode.


Extra care is required when the INI parameter opcache.validate_timestamps is set to False (0). In this case, Zend Opcache will not detect changes to your PHP script, so you must manually remove the Zend opcaches bytecode cache after changing the PHP file. This setting is very helpful to the product server on the line, but it will bring great inconvenience to the development. You can use the following php.ini configuration settings at development time to enable automatic file detection:

Opcache.validate_timestamps = 1

Opcache.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.