In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "PHP optimization acceleration component Opcache how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PHP optimization acceleration component Opcache how to use" it!
Opcache is a way to avoid the overhead of loading and parsing the PHP script every time by storing the precompiled bytecode (Operate Code) of the parsed PHP script in the shared memory. The parser can read the cached bytecode (Operate Code) directly from the shared memory, thus greatly improving the execution efficiency of PHP.
Why use opcode caching?
This starts with the life cycle of the PHP code. When you request a PHP script, you go through five steps, as shown in the following figure:
The Zend engine must read files from the file system, scan its dictionaries and expressions, parse files, create computer code to execute (called Opcode), and finally execute Opcode. Each request PHP script will perform the above steps once. If the PHP source code does not change, then the Opcode will not change. Obviously, there is no need to generate Opcode every time. Combined with the ubiquitous caching mechanism in Web, we can cache the Opcode. Wouldn't it be faster to access the cached Opcode directly in the future? the flow chart after enabling Opcode caching is as follows:
PHP opcode principle
Opcode is an intermediate language compiled by PHP scripts, such as ByteCode for Java or MSL for .NET. For example, you write the following PHP code:
Echo "Hello World"; $a = 1 + 1; echo $a Teng? >
When PHP executes this code, it goes through the following four steps (specifically, it should be PHP's language engine Zend)
1) Scanning (Lexing) to convert the PHP code into a language fragment (Tokens). 2) Parsing, which converts Tokens into a simple and meaningful expression. 3) Compilation, compile the expression to Opocdes. 4) Execution, execute Opcodes sequentially, one at a time, thus realizing the function of PHP script. PHP opcache introduction
Optimizer+ (Optimizer+ was renamed Opcache in mid-March 2013). OPcache improves the performance of PHP by storing precompiled bytecode in shared memory. The advantage of storing precompiled bytecode is that it saves the overhead of loading and parsing PHP scripts each time.
The OPcache extension has been bundled with PHP 5.5.0 and later. The OPcache library in the »PECL extension can be used for versions 5.3 and 5.4 of PHP 5.2.
PHP 5.5.0 and later
OPcache can only be compiled as a shared extension. If you disable the construction of the default extension with the-disable-all parameter, you must use the-enable-opcache option to turn on OPcache. After compilation, you can use the zend_extension directive to load the OPcache extension into PHP.
Recommended php.ini settings
Use the following recommended settings for better performance:
Opcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_files=4000opcache.revalidate_freq=60opcache.fast_shutdown=1opcache.enable_cli=1opcache.save_comments=0
You can also disable opcache.save_comments and enable opcache.enable_file_override. It is important to note that before using the above configuration in a production environment, it must be rigorously tested. Because there is a known problem with the above configuration, it can throw some exceptions to frameworks and applications, especially if comments are used in existing documents.
The following is the configuration description of opcache, which is the default configuration:
; opcache switch, turn off the time code is no longer optimized. Opcache. enable = 1; Determines if Zend OPCache is enabled for the CLI version of PHPopcache.enable_cli=1; OPcache shared memory size in megabytes. How much precompiled PHP code (in MB) can be stored in total; 128 opcache.memory strings is recommended 64; memory size used to store temporary strings is in megabytes; 8 opcache.internedstrings is recommended; maximum number of files cached is between 200 and 100000; 4000 opcache.maxcompilatedfiles is recommended 2000; if memory "waste" reaches the corresponding percentage, a restart will be initiated. Opcache.max _ wasted_percentage=5 When this instruction is turned on, Zend Optimizer + automatically appends the name of the current working directory to the script key to eliminate key naming conflicts between files with the same name. Turning off this directive will improve performance but cause damage to existing applications. Opcache.use _ cwd=0; open file timestamps verify that opcache.validate_timestamps=1; checks script timestamps for updates, in seconds. A setting of 0 causes OPcache to check for script updates for each request.; recommend 60opcache.revalidate freqroom2; optimizations that allow or disable file search in include_path .opcache.revalidate _ path=0 If disabled, the comments in the script file will not be included in the opcode cache file, which can effectively reduce the optimized file size. Disabling this configuration instruction may cause some applications or frameworks that rely on comments or annotations not to work properly, such as Doctrine,Zend Framework2, etc.; if disabled, comments will not be loaded even if comments are included in the file. This option can be used with opcache.save_comments to load comment content on demand. Opcache.load_comments=1; opens and closes quickly. Opening this will increase the speed of collecting memory when PHP Request Shutdown is opened. 1 opcache.fastshutdowndownload1 is recommended; optimization features that allow override files to exist (file_exists, etc.). Opcache.enable _ file_override=0; defines how many optimization processes are started. Opcache.optimization _ level=0xffffffff Enable this Hack to temporarily resolve the "can't redeclare class" error. Opcache.inherited_hack=1; enable this Hack to temporarily resolve the "can't redeclare class" error. Opcache.dups_fix=0; removes the cache of large files through the file size screen, and all files are cached by default. Opcache.max_file_size=0; checks the cache check every N requests. The default value of 0 means that checking is disabled. Since calculating the check value is detrimental to performance, this instruction should be turned on tightly during development and debugging. After opcache.consistency_checks=0; is not accessed from the cache, how long (in seconds) it will be scheduled to restart. Opcache.force_restart_timeout=180; log records level. By default, only fatal error and error.;opcache.error_log=; write error messages to the server (Apache, etc.) log. The preferred background for opcache.log_verbosity_level=1; memory sharing. Leave blank to let the system choose.; opcache.preferred_memory_model=; protects shared memory from accidental writes when running php scripts, and is only useful for debug.; opcache.protect_memory=0
Finally, let's talk about the pitfalls that you should pay attention to when using opcache to accelerate php:
Opcache relies on the modify time of the PHP file as the detection condition for the file to be modified, which leads to two problems.
The first problem is that when doing a version rollback, because the file modification time after the version rollback is a little earlier than the file time of the existing opcache cache, it may cause opcache not to clear the cache and require manual reload.
The second problem is that when a version is released, it is usually in the sync mode. It may be cached by opcache half of the time when the file is released, and a program error will be reported when the user accesses it. This is mainly because the file content is half cached, but the timestamp of the file will not change, so even if the opcache is detected, the new file will not be read, and manual reload is needed.
Not only can reload solve these two problems, but the opcache cache can also be cleared by calling the interface of opcache.
You can use the opcache_reset () or opcache_invalidate () function to manually reset the OPcache.
Opcache_reset (): this function resets the entire bytecode cache, and after calling opcache_reset (), all scripts will be reloaded and reparsed the next time they are clicked.
Opcache_invalidate (): this function invalidates the bytecode cache of the specified script. If force is not set or FALSE is passed in, the script's cache will be invalidated only if the modification time of the script is newer than the time of the corresponding bytecode.
However, it is not recommended. Individuals call opcache_reset () to clear the cache after code release in the production environment (the test does clear the cache). There have been strange problems (applications with high traffic), and then they decisively give up and use the reload method.
Thank you for your reading, the above is the content of "how to use PHP's optimization acceleration component Opcache". After the study of this article, I believe you have a deeper understanding of how to use PHP's optimization acceleration component Opcache, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.