The operation mechanism of PHP is determined, in fact, PHP in the run phase we can also be cached to improve the efficiency of the program, which is what we often call the opcode cache.
1. Brief introduction of PHP operating mechanism
(Because this article is written opcode cache, so here is a brief overview, behind will be dedicated to write a secret PHP operating mechanism.) )
a). php files come through the browser
b) The request is given to SAPI, and then the SAPI layer transfers control over to PHP
c) zend_language_scanner to scan the code and parse the PHP code into a series of tokens arrays
d) Zend_language_parser convert a series of useless code, such as a sequence of tokens from C-step, into a series of expressions
e) opcode return zend_op_array pointer after compiler phase generation
f) Zend_vm_execute executes the opcode based on the incoming Zend_op_array pointer and returns the result to the output
(from the Internet, but the description is very good jiehuaxianfo)
2, OpCode Introduction
OpCode is the abbreviation for operation code (opcode), which is actually the first bar C), D), E) step produced by an intermediate code,
OpCode is a four-tuple, (opcode, OP1, OP2, result), which represents the opcode, the first operand, the second operand, and the result.
Such as:
<?phpecho "Taobao search blog";? >
The corresponding tokens
Array ( [0] = array ( [0] = 367 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[1] = <?php &NBSP;&NBSP;[2] = 1 ) [1] = array ( [0] = 316 [1] = ECHO&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[2] = 1 ) [2] = array ( [0] = 370 & nbsp; [1] => &NBSP;&NBSP;&NBSP;&NBSP;[2] = 1 ) &NBSP;[3] = array ( [0] = 315 &NBSP;[1] = "Taobao Search blog" [2] = > 1 ) [4] =; &NBSP;&NBSP;[5] = array ( [0] = 370 &NBSP;&NBSP;[1] => [2] = 1 ) [6] = array ( [0] = 369 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[1] =?> &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;[2] = 1 ) )
The
Corresponding opcode is:
line # * op fetch ext return Operands--------------------------------------------------------------------------------- 2 0 > echo ' Taobao+search+blog ' 4 1 > return 1 2* > Zend_handle_exception
3. Use APC to opcode cache
A) assume that the PHP path is/home/ad/php
There are many software caches for opcode (APC, eacclerator, Xcache, Zend Platform), which mainly introduces APC
APC provides two cache functions, namely cache opcode (target file), which we call Apc_compiler_cache. It also provides interfaces for PHP developers to host user data in memory, which we call Apc_user_cache. Here we mainly discuss the configuration of Apc_compiler_cache.
: Http://pecl.php.net/package/APC
The latest version is apc-3.1.6.tgz
wget HTTP://PECL.PHP.NET/GET/APC-3.1.6.TGZTAR-ZXVF APC-3.1.6.TGZCD Apc-3.1.6/home/ad/php/bin/phpize./configure-- ENABLE-APC--enable-apc-mmap--with-php-config=/home/ad/php/bin/php-configmakemake Install
Edit PHP.ini
Adding an APC configuration
[apc]extension=apc.soapc.enabled=1apc.shm_segments = 1apc.shm_size = 128apc.ttl = 0apc.user_ttl = 7200apc.num_files_ hint = 1000apc.write_lock=1apc.stat = 0apc.max_file_size=1mapc.filters = A.php,b.phpapc.cache_by_default=1
Re-Apache is OK.
4, the analysis of common parameters
apc.enabled turn on APC set to 0 off, 1 to open
Apc.shm_segments number of shared memory blocks
Apc.shm_size shared memory size, but M
So it's clear that the total number of shared memory is apc.shm_segments*apc.shm_size
Apc.num_files_hint the number of opcode allowed to be cached
Apc.stat for 1 will automatically check whether the opcode corresponding PHP files are updated, updates will be automatically updated. If you set it to 0, you won't be able to check it. This will improve the efficiency of your APC, but you will have to restart Apache if the PHP changes take effect, or use the function apc_cache_clear () to empty the cache
Apc.ttl opcode cache expiration time, set to 0 to not expire, if not 0 will check the time between two requests, if the time is greater than the set value then the opcode cache will be updated
Apc.write_lock indicates that multiple processes update a opcode cache at the same time so that only the first one takes effect, which effectively avoids write conflicts
Apc.max_file_size files that exceed the set value are not cached
Apc.filters requires special files, multiple files separated by commas (,)
Apc.filters is used in combination with Apc.cache_by_default,
When Apc.cache_by_default is 1 o'clock apc.filters file is not cached, only apc.filters files are cached when Apc.cache_by_default is 0
5:APC Use Example
case one: Using PHP's APC module to make an upload progress bar
https://www.ttlsa.com/php/php-apc-upload-process/
php's opcode cache