The use of opcache in php

一个新手
Release: 2023-03-15 18:54:02
Original
1925 people have browsed it

It is well known that php is an interpreted language, and its execution can be divided into the following processes:

  1. Scanning (Lexing), which converts PHP code into language fragments (Tokens)

  2. Parsing, converts Tokens into simple and meaningful expressions

  3. Compilation, compiles expressions into Opocdes

  4. Execution, executes Opcodes sequentially, one at a time, thereby realizing the functions of PHP scripts.

In this case, repeated requests for the same file will require continuous parsing, compilation and execution of PHP scripts, consuming too many resources.

PHP traditional operating mode:

The use of opcache in php

In order to solve this problem, opcode caching technology came into being.

So what is opcode cache?

When the interpreter completes the analysis of the script code, it generates intermediate codes that can be run directly, also called operation codes (Operate Code, opcode). Opcode caching is to cache the compiled Opcode code in the memory. The next time you execute this script, you will directly find the opcode code in the cache and directly execute the last step without the need for intermediate steps. Its purpose is to avoid Repeat compilation to reduce CPU and memory overhead.

When the interpreter completes the analysis of the script code, it generates intermediate codes that can be run directly, also called operation codes (Operate Code, opcode). Opcode caching is to cache the compiled Opcode code in the memory. The next time you execute this script, you will directly find the opcode code in the cache and directly execute the last step without the need for intermediate steps. Its purpose is to avoid Repeat compilation to reduce CPU and memory overhead.

Cache operation mode:

The use of opcache in php

opcache is an opcode caching tool.

The biggest difference between Opcache and other caching tools, which can also be said to be its advantage, is that it has entered the PHP source code tree and become an official product. It will always be updated with PHP, while the others have not been updated for a long time. Currently, for New versions of PHP are not supported or have poor compatibility. Secondly, after testing, opcache is also more efficient than other tools.

So how to enable the use of opcache?

Now that opcahce has entered the PHP source tree, as a built-in tool, the opening method must be very simple. The following is the recommended configuration:

zend_extension=.././...php_opcache.dll(路径)
opcache.memory_consumption=128  
 #共享内存opcache.interned_strings_buffer=8   
 #存储临时字符串opcache.max_accelerated_files=4000   
 #缓存文件数opcache.revalidate_freq=60  
 #缓存时间opcache.fast_shutdown=1opcache.enable_cli=1 opcache.enable=1   
 #是否开启opcode缓存
Copy after login

You only need to remove the previous; .

Of course opcache has many configurations, so I won’t list them all here. The above ones are standard configurations and can meet general needs.

Theoretically, the larger the PHP file and the more complex the logic, the more obvious the effect of opcache will be, and it will play a great role in speed and anti-concurrency.

The test results in the local environment are as follows (computer performance is limited):

The use of opcache in php

Server test:

The use of opcache in php

The use of opcache in php

There is a significant improvement in efficiency.

It should be noted that since opcache will cache the opcode executed for the first time in the file, it should not be opened in the test environment. The reason is that you know that the code is changed during development, but the result is different. Unchanged…………..

To manually clear the opcache cache, you need to run opcache_reset() to clear it, so if opcache is configured online and needs to be cleared, you can use this function.

Reprinted from: http://qqfe.org/archives/240?utm_source=tuicool&utm_medium=referral

众所周知php是一种解释型语言,它的执行可分为如下几个流程:

  1. Scanning(Lexing) ,将PHP代码转换为语言片段(Tokens)

  2. Parsing, 将Tokens转换成简单而有意义的表达式

  3. Compilation, 将表达式编译成Opocdes

  4. Execution, 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能。

这样一来的话,对于同一个文件,反复请求,就要不断解析、编译和执行PHP脚本,消耗过多资源。

php传统运行方式:

The use of opcache in php

为了解决这个问题,opcode缓存技术应运而生。

那么什么是 opcode 缓存?

当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode)。Opcode 缓存就是将编译好的Opcode码缓存在内存中,下次再执行这个脚本时,直接会去找到缓存下的opcode码,直接执行最后一步,而不再需要中间的步骤了,其目地是避免重复编译,减少 CPU 和内存开销。

当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode)。Opcode 缓存就是将编译好的Opcode码缓存在内存中,下次再执行这个脚本时,直接会去找到缓存下的opcode码,直接执行最后一步,而不再需要中间的步骤了,其目地是避免重复编译,减少 CPU 和内存开销。

缓存运行方式:

The use of opcache in php

opcache就是一种opcode缓存工具。

Opcache其他的缓存工具最大的不同,也可以说是优势,就是它已经进入php源码树,成为官方产品,会一直跟着php更新,而其他的已经很久不更新了,目前对于新版的php都不支持或者兼容性很差。其次,经过测试,opcache的效率也比其他工具高。

那么如何开启使用opcache呢?

既然opcahce已经进入php源码树,那么作为内置的工具,开启方式肯定很简单了,下面便是推荐配置:

zend_extension=.././...php_opcache.dll(路径)
opcache.memory_consumption=128   
#共享内存opcache.interned_strings_buffer=8  
 #存储临时字符串opcache.max_accelerated_files=4000   
 #缓存文件数opcache.revalidate_freq=60  
 #缓存时间opcache.fast_shutdown=1opcache.enable_cli=1 opcache.enable=1   
 #是否开启opcode缓存
Copy after login

你只需要将前面的;去掉就可以了。

当然opcache有很多配置,这里就不一一列举,上面这几条是标配,可以满足一般需求。

理论上讲,php文件越大,逻辑越复杂,那么opcache的效果就越明显,而且在速度和抗并发上有非常大的作用。

在本地环境测试结果如下(电脑性能有限):

The use of opcache in php

服务器测试:

The use of opcache in php

The use of opcache in php

有明显的效率提升。

需要注意到是,opcache既然会缓存住文件第一次执行的opcode,那么在测试环境就不要开了,原因你懂得,开发时代码改来改去,结果却不变………..

要手动清除opcache缓存,需要运行opcache_reset()来清除,所以线上如果配置了opcache,需要清除的话可以用这个函数。

The above is the detailed content of The use of opcache in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template