Home  >  Article  >  Backend Development  >  Detailed explanation of OPcache extension in PHP

Detailed explanation of OPcache extension in PHP

青灯夜游
青灯夜游forward
2020-07-27 17:57:312791browse

Detailed explanation of OPcache extension in PHP

OPcache improves the performance of PHP by storing the precompiled bytecode of PHP scripts in shared memory. The advantage of storing precompiled bytecode is that it eliminates the need to load and The overhead of parsing PHP scripts. Versions after PHP 5.5 are bound to this extension by default. Previous versions of PHP 5.2, 5.3 and 5.4 can use the OPcache library in the » PECL extension.

Explanation of configuration items

opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

This configuration item is recommended by the PHP manual and is set in the middle of php.ini. About each configuration item The description is as follows:

  • opcache.memory_consumption: The shared memory size of OPcache, in megabytes.
  • opcache.interned_strings_buffer: The size of memory used to store reserved strings, in megabytes. Versions of PHP prior to 5.3.0 ignore this configuration directive.
  • opcache.max_accelerated_files: The upper limit of the number of script files that can be stored in the OPcache hash table. The real value is the first prime number found in the prime number set {223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987} that is greater than or equal to the set value. The minimum value range of the setting value is 200, the maximum value is 100000 before PHP 5.5.6, and 1000000 after PHP 5.5.6.
  • opcache.revalidate_freq : The period to check whether the script timestamp has been updated, in seconds. Setting to 0 causes OPcache to check for script updates on every request. If the opcache.validate_timestamps configuration directive is set to disabled, this setting will be ignored.
  • opcache.validate_timestamps : If enabled, OPcache will check whether the script has been updated every opcache.revalidate_freq set seconds. If this option is disabled, you must manually reset OPcache using the opcache_reset() or opcache_invalidate() functions, or restart the web server for file system changes to take effect.
  • opcache.fast_shutdown : If enabled, fast stop will be used to resume events. The so-called quick stop resumption event refers to the memory management module that relies on the Zend engine to release the memory of all requested variables at once, rather than releasing each allocated memory block in sequence. Starting with PHP 7.2.0, this configuration directive has been removed. Handling of fast-stop resumption events has been integrated into PHP, and PHP will handle these resumption events automatically whenever possible.
    For more configuration item descriptions, please refer to the OPcache manual.

OPcache function

OPcache provides some built-in functions for controlling the cache status of bytecodes. The prerequisite for use is that Turn on the OPcache extension.

  • opcache_compile_file: You can compile and cache the PHP script without running it. If you use the parameter FILE separately, it is required and FILE is the script. Path, for example: opcache_compile_file('index.php')
  • opcache_get_configuration: Get all current configuration information of opcache
  • opcache_get_status: Get opcache's Current cache information, the information includes whether the shared memory space is full, etc.
  • opcache_invalidate: Invalidate the script cache, there are two parameters, script (script path information) is required, force (boolean) is optional , if the force parameter is true, the cache is forced to be discarded. If it is false or not filled in, then the script's cache will be invalid only when the modification time of the script is newer than the corresponding bytecode time.
  • opcache_is_script_cached: Check whether the script is cached in OPCache, the parameter FILE (file path) is required
  • opcache_reset: Recharge all opcache caches, wait Next time you perform compilation

Development Notes

In the local development environment, it is recommended to turn off the OPcache extension, the cache may make the local Debugging creates many problems and reduces development efficiency.

Knowledge expansion about shared memory

Shared memory is an efficient way to exchange data between applications in the same machine. The execution efficiency of memory for programs is higher than that of disk I/O, so reasonable use of memory can improve the efficiency of programs. Its application scenarios include web requests, database queries, template engine caching, and the OPcache compilation cache for PHP mentioned in the current article.

Related tutorial recommendations: "PHP Tutorial"

The above is the detailed content of Detailed explanation of OPcache extension in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete