PHP uses Zend Opcache optimization acceleration and Cache cleanup Summary

Source: Internet
Author: User
Tags php script zend
Brief introduction

Bytecode caching is not a new feature of PHP, and there are many independent extensions that can implement caching, such as PHP cache (APC), Eaccelerator,ioncube and XCache, and so on. But so far, these independent extensions have not been integrated into the PHP core. All after php5.5.0, PHP has built-in bytecode caching, called Zend Opcache.

Zend Opcache was formerly Zend Optimizer +, renamed Opcache in 03, providing faster PHP execution through opcode caching and optimization. He will store the precompiled PHP files in shared memory for later use, to avoid reading files from disk in the repetitive process of interpretation, reducing the time and memory consumption. The Zend Opcache module extension is available in php5.5 and later versions, but needs to be opened and configured when needed. We can also use Opcache in the php5.2-5.4 version, but we need to install it ourselves.

What is opcode caching

So what is byte-code caching? PHP is an interpreted language, PHP interpreter executes PHP script will parse the PHP script code, the PHP script code to compile a series of directly run the intermediate code, also known as the opcode (Operate code,opcode). These opcode are then executed.

Every time you request PHP files, so it consumes a lot of resources, if every HTTP request PHP must parse, compile and run the script, consumes more resources.

The goal of the Opcode cache is to avoid duplication of compilation and reduce CPU and memory overhead. It is important to note that if the performance bottleneck of dynamic content is not in CPU and memory, but in I/O operations, such as the disk I/O overhead of database queries, the performance gains of the opcode cache are very limited.

Modern opcode buffers (optimizer+,apc2.0+, others) are stored using shared memory and can be executed directly from the file without the "deserialization" code before execution. This leads to significant performance acceleration, which typically reduces the overall server's memory consumption and has few drawbacks.

Install (skip above php5.5.0)

In PHP 5.5.0 and later versions, PHP has the Opcache function in the expanded form embedded in the release version, the default does not turn on opcache acceleration, we need to manually open. For previous versions, Opcache can be installed and configured as a PECL expansion library.

Installation under Window

1. Download extension: windows.php.net/downloads/pecl/releases/opcache/

2. Place the Php_opcache.dll in the Php/ext directory

3. Modify the [PHP] configuration under php.ini (note that the path is to be modified to your own):

=="c:/xxx/php/ext/php_opcache.dll"
4. Add the [Opcache] configuration under php.ini:
[Opcache] opcache.memory_consumption=opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000opcache.revalidate_freq=  Opcache.fast_shutdown=1opcache.enable_cli=1
5. Restart the Apache service to check if the Opcache is open successfully

Installation under Linux

SOURCE Installation

wget http://pecl.php.net/get/zendopcache-7.0.5.tgztar zxvf zendopcache-  7.0. 5 . TGZCD Zendopcache-7.0. 5/path/to/php/bin/phpize. /configure--with-php-config=/path/to/php/bin/php-configmake doinstall 
under PHP.ini [PHP] Add the following configuration:
Zend_extension=php_opcache.so
under PHP.ini [Opcache], add:
opcache.memory_consumption=opcache.interned_strings_buffer=8opcache.max_ Accelerated_files=4000opcache.revalidate_freq=opcache.fast_shutdown =1opcache.enable_cli=1
pecl Version Installation
Yum Install Php-pecl-zendopcache
The configuration file for the Opcache generated at the time of installation is located in the default/ETC/PHP.D directory:
opcache-default.blacklist Opcache.ini
Modify the configuration:
VI /etc/php.d/opcache.ini  
Control changes:
opcache.memory_consumption=opcache.interned_strings_buffer=8opcache.max_ Accelerated_files=4000opcache.revalidate_freq=opcache.fast_shutdown =1opcache.enable_cli=1
no need to modify php.ini, restart Apache service.

Common configuration
The switch opens the opcache.enable.=1; open Cliopcache.enable_cli=1; available memory, as appropriate, in: Mbopcache.memory_consumption=528; Zend Optimizer+The total memory of the string in the staging pool. (unit: MB) Opcache.interned_strings_buffer=8, limit to multiple cache files, hit less than -%, you can try to raise this value Opcache.max_accelerated_files=10000Opcache will check the file modification time in a certain time, here set the check time period, the default is2, positioning as seconds opcache.revalidate_freq=1Turn on quick close to open this shutdown the memory recovery rate will increase when PHP Request is Opcache.fast_shutdown=1checks whether the script timestamp has an updated period, in seconds. Set to0causes Opcache to check for script updates for each request. Opcache.revalidate_freq=0if enabled, the Opcache will check if the script is updated at the number of seconds set per Opcache.revalidate_freq. If you disable this option, you must use Opcache_reset () or opcache_invalidate () function to reset the opcache manually, or you can make the file system changes take effect by restarting the WEB server. Opcache.validate_timestamps=0  
Note: If the instructions for setting Opcache Opcache.validate_timestamps are set to 0, then Zend Opcache is unaware of the changes in the PHP script, we must manually empty the Zend Opcache cached bytecode, Let him see the changes in the PHP script. This setting is suitable to be set to 0 in the production environment, preferably set to 1 in the development environment.

We can configure this to enable the automatic re-authentication cache feature:

opcache.validate_timestamps=1opcache.revalidate_freq=0

More configuration instructions can be seen here: http://php.net/manual/zh/opcache.configuration.php

Common functions

Zend Opcache is easy to use because it will run automatically when it starts. Zend Opcache automatically caches pre-compiled PHP bytecode in memory and executes the corresponding bytecode if the byte code of a file is cached. Common functions for the Zend Opcache extension:

true ) #清除单个缓存opcache_reset (); #清空缓存opcache_get_status (); #获取缓存的状态信息opcache_get_configuration (); #获取缓存的配置信息

Above

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.