php of Opcache

opcache principle

1. What Opcache that?

Opcache by a PHP script parsed precompiled bytecode (Operate Code) stored in shared memory each time to avoid the overhead of loading and parsing PHP scripts, parsers can be directly read from the shared memory is cached word section code (Operate code), which greatly improve the efficiency of PHP.

2. What is the Operate Code?

After the completion of the analysis of the script interpreter code, which generates the intermediate code can be put directly run, also known as the opcode (Operate Code, opcode). Opcode cache is to head to avoid duplication of compilation, reducing CPU and memory overhead. If the performance bottleneck of dynamic content is not the CPU and memory, but in I / O operations, such as database queries to bring the disk I / O overhead, then the opcode cache performance improvement is very limited. But since opcode cache can bring reduce CPU and memory overhead, which is always a good thing. Modern operating code register (Optimizer +, APC2.0 +, other) to use shared memory for storage, and may be executed directly from a file, rather than before execution "deserialization" code. This will bring significant performance boost, usually in particular reducing the overall server memory consumption of high flow and high concurrency, and few drawbacks.

3. Why use Opcode cache?

This was from the life cycle of PHP code, PHP script request will go through five steps, as shown below:

 

 

Zend engine must be read from the file system file, scan its dictionary and expressions, parse documents, create computer code to be executed (called Opcode), the last execution Opcode. Each request will be executed PHP script steps above again, if the PHP source code does not change, then Opcode will not change, obviously it is not necessary every time the heavy-line build Opcode, combined with the ubiquitous Web caching mechanism, we can put down Opcode cache , directly after the access to the cache is faster Opcode it not, then the flowchart Opcode caching enabled as follows:

 

 

Thus after use the cache Operate Code, PHP code opcode executed directly after the direct access, three intermediate steps will be omitted thus substantially increase the efficiency of PHP code

Third, the installation and use of Opcache

1, the installation

1

2

3

4

5

6

7

8

9

10

当你的PHP版本低于7.0时,你可以去http://pecl.php.net/package/ZendOpcache 根据自己的PHP版

本下载相应的opcache版本。比如PHP5.5:http://pecl.php.net/get/zendopcache-7.0.5.tgz

tar -zxvf zendopcache-7.0.5.tgz

cd zendopcache-7.0.5

phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make

make install

这是会在php的扩展文件夹内生成opcache.so

PHP7.0之后自带opcache.so无需安装

2, using

1

2

3

在php.ini加入zend_extension=opcache.so;

注意:这里是zend_extension不是extension,关于zend_extension和extension的区别可以去查阅

相关资料

3, Configuration

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

关于opcache的详细参数配置比较核心的参数如下:

opcache扩展位置

zend_extension=opcache.so

启用opcache

opcache.enable=1

使用共享内存大小

opcache.memory_consumption=200

字符串缓存大小

opcache.interned_strings_buffer=8

最大缓存文件数量

opcache.max_accelerated_files=8000

出现异常,立即释放全部内存

opcache.fast_shutdown=1

最大允许占用内存百分比,超过此限制会重启进程

opcache.max_wasted_percentage=20

如果置为1,则将当前路径加入到文件key中,以避免可能产生的同文件名的文件key冲突

opcache.use_cwd=1

文件检测周期

revalidate_freq=3600

启用文件缓存时间戳

opcache.validate_timestamps=1

Guess you like

Origin www.cnblogs.com/xingxia/p/php_opcache.html