[ModernPHP] Chapter 2 new feature 6 ZendOPcache
Zend OPcache
Bytecode caching technology is not new to PHP. We already have Alternative PHP Cache (APC), eAccelerator, ionCube, and XCache independent Extensions. they can all be used as our optional solutions. But they are not found in every core release version of PHP. Until now, PHP 5.5.0 has its built-in bytecode cache: Zend OPcache.
First, let me explain what bytecode is and its importance. PHP is an interpreted 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 finally executes these bytecode. The PHP file repeats the preceding steps every time it is requested. This is a waste of time, especially when the PHP script needs to be parsed, compiled, and executed again and again every HTTP request. If we have a way to cache these compiled bytecode, we can shorten the application response time and reduce the pressure on system resources. You are 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 for each request, but can directly read and execute the compiled bytecode from the memory. This greatly saves time and greatly improves application performance.
Enable Zend OPcache
Zend OPcache is disabled by default. you need to enable Zend OPcache explicitly when installing and compiling PHP.
If you are using a VM, make sure you choose an excellent service provider that provides PHP 5.5.0 and later versions and enables Zend OPcache.
If you compile PHP by yourself (assuming you are using VPS or server hosting), you must add a parameter after the PHP./configure command
-- Enable-opcache
After PHP compilation is complete, you also need to specify the Zend OPcache extension path in the phpini file. refer to the following example:
Zend_extension =/path/to/opcache. so
After PHP compilation is successful, the Zend OPcache extension file path will be displayed immediately. If you forget to do this as I said, you can also execute the following command to obtain the path addresses of all extensions in PHP:
Php-config -- extension-dir
If you are using the unparalleled Derick Rethans tool for developing Xdebug, the Zend OPcache extension must be loaded before the Xdebug extension in the php. ini file.
After you update the php. ini file and restart the PHP process, you can use it. If you need to confirm that Zend OPcache is correctly installed, you can create a php file containing the following content:
View the php file in the browser and pull down the scroll bar until you see the Zend OPcache extension section, as shown in Figure 2-2. If this message is not displayed, Zend OPcache is not running.
Figure 2-2 Zend OPcache INI settings
Configure Zend OPcache
When Zend OPcache is enabled, you can configure Zend OPcache in the php. ini configuration file. Below are my favorite OPcache settings:
Opcache. validate_timestamps = 1 // use "0" in the production environment"
Opcache. revalidate_freq = 0
Opcache. memory_consumption = 64
Opcache. interned_strings_buffer = 16
Opcache. max_accelerated_files = 4000
Opcache. fast_shutdown = 1
Want to know