OpCode Cache for PHP

Source: Internet
Author: User

PHP is well known as an interpreted language, and its execution can be divided into the following processes:

    1. Scanning (lexing), converting the PHP code to a language fragment (Tokens)

    2. parsing, converting tokens into simple and meaningful expressions

    3. compilation, compile the expression into Opocdes

    4. 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.

Related Article

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.