In large-scale system development, Cache is undoubtedly crucial. In the PHP world, although there are not as many cache solutions to choose from as in Java, there are still some mature solutions, from "advanced PHP programming" I learned the following:
1. Language-level optimization: PHP has many engine-level APIs. Through these APIs, the behavior of engine execution can be changed to achieve the purpose of optimizing operation. Among them, the most worthwhile thing to do is to cache the compilation results. As we all know, every time PHP is executed, it needs to go through the process of source code -> compilation -> intermediate code -> engine execution. For some large applications, a considerable amount of time is spent on useless compilation (not just the PHP that accesses the page) Files need to go through the compilation process. When require() and include() are used in the script, some files included need to be compiled). By caching the compilation results, the performance of the system can be greatly improved (proportional to the complexity and scale of the system).
The three mainstream tools in the PHP world that can implement compile cache are:
The Zend Accelerator—A commercial, closed-source, for-cost compiler cache produced by Zend Industries
The ionCube Accelerator—A commercial, closed-source, but free compiler cache written by Nick Lindridge and distributed by his company, ionCube
APC—A free and open-source compiler cache written by Daniel Cowgill and George Schlossnagle
APC installation method, APC is included in PECL, the specific installation is as follows 190-823 190-802:
Afterrunning the command
#pear install apc
, in php.ini Add:
extension = /path/to/apc.so
to the file. In this way, the installation is completed. When php is run next time, APC will be automatically activated and the compilation results will be cached in the shared memory. The next time it is executed, Obtain the editing results directly from the memory and execute them without compiling again.
Question: Can APC automatically recompile PHP that has changed since the last compilation?
2. PHP code optimization: Use some tools to generate high-quality intermediate code after compilation. The details are as follows:
The Zend Optimizer is a closed-source but freely available optimizer.
The ionCube accelerator contains an integrated optimizer.
proof-of-concept optimizer in PEAR.
The main functions of the optimizer:
1. Remove useless code, such as dead code that will never be executed.
2. Calculation of constants, for example, changing $seconds_in_day = 24*60*60 directly to $seconds_in_day = 86400;
3. Other code optimization functions, such as this statement:
$count++;
will be optimized to ++$count, making execution Faster. Of course, if the statement is $i = $count++; no optimization will be done