PHP is well known as an interpreted language, and its execution can be divided into the following processes:
Scanning (lexing), converting the PHP code to a language fragment (Tokens)
parsing, converting tokens into simple and meaningful expressions
compilation, compile the expression into Opocdes
Execution, executes opcodes sequentially, one at a time, thus realizing the function of PHP script.
This way, for the same file, repeated requests, it is necessary to constantly parse, compile and execute PHP scripts, consuming too much resources.
PHP Traditional operation mode:
In order to solve this problem, opcode cache technology came into being.
So what is the opcode cache?
When the interpreter finishes parsing the script code, it generates intermediate code that can be run directly, also known as the opcode (Operate code,opcode). Opcode Cache is the compiled Opcode code cached in memory, the next time you execute this script, will go directly to find the cache Opcode code, directly perform the last step, and no longer need the intermediate steps, its goal is to avoid duplication of compilation, reduce CPU and memory overhead.
When the interpreter finishes parsing the script code, it generates intermediate code that can be run directly, also known as the opcode (Operate code,opcode). Opcode Cache is the compiled Opcode code cached in memory, the next time you execute this script, will go directly to find the cache Opcode code, directly perform the last step, and no longer need the intermediate steps, its goal is to avoid duplication of compilation, reduce CPU and memory overhead.
How the cache runs:
Opcache is a opcode caching tool.
Opcache Other caching Tools The biggest difference, also can be said to be the advantage, is that it has entered the PHP source tree, become the official product, will always follow the PHP update, and the other has been a long time not updated, currently for the new version of PHP is not supported or compatibility is poor. Second, after testing, Opcache is also more efficient than other tools.
So how do you start using Opcache?
Since Opcahce has entered the PHP source tree, then as a built-in tool, the opening method must be very simple, the following is the recommended configuration:
zend_extension=. /./...php_opcache.dll (path) opcache.memory_consumption=128 #共享内存opcache. interned_strings_buffer=8 # Stores the temporary string opcache.max_accelerated_files=4000 #缓存文件数opcache. revalidate_freq=60 #缓存时间opcache. fast_shutdown= 1opcache.enable_cli=1 opcache.enable=1 #是否开启opcode缓存
All you have to do is to take the previous one and get rid of it.
Of course Opcache has a lot of configuration, here is not listed, above these are standard, can meet the general demand.
Theoretically, the larger the php file, the more complex the logic, then the Opcache effect is more obvious, but also in the speed and anti-concurrency has a very large role.
Test results in the local environment are as follows (computer performance is limited):
Server test:
There is a noticeable increase in efficiency.
It should be noted that since the Opcache will cache the first execution of the file opcode, then in the test environment will not open, the reason you understand that the development of the code to change, the result is not changed ...
To manually clear the Opcache cache, you need to run Opcache_reset () to clear, so if you configure Opcache on the line, you can use this function if you need to clear it.