PHP7 Opcache Cache Cleanup issues
Background
Opcache provides faster php execution through opcode caching and optimization.
When the business is operating in the PHP7 environment, the Opcache extension is configured in the PHP7 environment in order to improve the performance of the request.
Business after updating the code, when accessing the business system, the prompt cannot find the corresponding file or requested content or the old content before the update.
Webserver restart, the requested files are all up-to-date, the problem seems to be resolved.
Problem analysis
According to the analysis of the phenomenon, the code update after the request can not find the new files, especially in the request of the existing files before the update, then it may be related to the cache, considering that the business code is not related to the logic, the shutdown opcache configuration problem will no longer appear, basically can locate the problem in the Opcache configuration.
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/9C/B1/wKiom1l0u8LxZ6VcAAA6HvSBfs0135.png-wh_500x0-wm_ 3-wmp_4-s_608633139.png "class=" Amplify "style=" Border:medium none;vertical-align:top;height:auto; "Title=" 1500543707_60_w1002_h134.png "alt=" Wkiom1l0u8lxz6vcaaa6hvsbfs0135.png-wh_50 "/>
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/9C/B1/wKioL1l0u8Kz-X7GAABTlh-FtMc965.png-wh_500x0-wm_ 3-wmp_4-s_2036350728.png "title=" 1500543717_97_w838_h306.png "alt=" wkiol1l0u8kz-x7gaabtlh-ftmc965.png-wh_50 "/ >
cat/usr/local/php/etc/subconfig/opcache.inizend_extension=opcache.soopcache.enable=1opcache.revalidate_freq= 0opcache.validate_timestamps=0opcache.max_accelerated_files=7963opcache.memory_consumption=192opcache.interned _strings_buffer=16opcache.fast_shutdown=1opcache.enable_cli=1
opcache.enable Enable opcode cache, default to "1"
If this option is disabled, the code is not optimized and cached. Using the Ini_set () function at run time can only disable opcache.enable settings, and this setting cannot be enabled. If you try to enable this setting in the script, a warning is generated.
opcache.enable_cli enables opcode caching only for CLI versions of PHP.
It is usually used for testing and debugging.
opcache.revalidate_freq=0 Checks if the script timestamp has an updated period, in seconds.
Setting to 0 causes Opcache to check for script updates for each request.
Opcache.validate_timestamps= 0 if enabled, Opcache checks whether the script is updated every opcache.revalidate_freq the number of seconds that are set.
If you disable this option, you must manually reset the Opcache using the Opcache_reset () or opcache_invalidate () function, or you can make the file system changes take effect by restarting the WEB server.
The initial configuration is:
Opcache.revalidate_freq=60,opcache.validate_timestamps=1
That is, every 60 seconds to detect the update bytecode cache, business code updates may take 60 seconds to access to the latest content, there is no initial access to new content.
How code is updated
PHP code is updated in two ways, one is to overwrite the files in the directory of the webserver configuration to update, one is to deploy a full-scale package directory each time, and then the soft link to the webserver specified directory.
The first way to overwrite updates, if you use automatic cleanup of Opcache cache content after an expiration time, if there is a delay in the update operation, the new and old code files will appear mixed together.
After the second full-scale package catalog is published, the soft links to webserver specify the path, although there will be no new and old file blending problems, but when not automatically cleaned, even if webserver has been linked to the webserver corresponding directory, the business access to the old file.
Problems with code caching
The current way to synchronize directory files with Rsync is the main way we update the code, initially using every 60s time to clean up the Opcache cache files, files updated within 60s will not take effect, resulting in the business feedback code updated after the problem of access.
The problem of updating the code cache with timed updates, as well as the update of the file when the code file is published, there will be a mixture of 60s old and new files in the cache.
According to the relevant researcher recommendation, if the update is done by overwriting the code file, it is more appropriate to manually clean up the cache.
Opcache.validate_timestamps=0
That is, set Oopcache.validate_timestamps to 0.
With the Opcache.validate_timestamps value of 0 configured, you must manually empty the Zend Opcache cached bytecode to access the latest file content. Suitable to be set to 0 in a production environment, but is inconvenient in the development environment and can be configured to enable the automatic validation cache feature in the development environment:
Opcache.validate_timestamps=1
Opcache.revalidate_freq=0
Manually clean up the cache
In addition to the process of restarting PHP-FPM to clean up the Opcache cache, the Opcache functions involved in
Manual cleanup of the cache are: Opcache_reset () and Opcache_invalidate ().
booleanvoid
在调用 opcache_reset() 之后,所有的脚本将会重新载入并且在下次被点击的时候重新解析。
in some (most?) systems,
Php s CLI have a separate opcode cache to the one used by the Web server,
or php-fpm Process,which means running Opcache_reset () in the CLI,
Won ' t reset the webserver/fpm opcode cache, and vice-versa.
Curve to salvation, use the command line to clean up the PHP-FPM Opcache cache:
#!/bin/bashcgi-fcgi-v >/dev/null 2>&1| | Yum--enablerepo=epel install fcgi-y >/dev/null 2>&1echo ' <?php opcache_reset (); echo "ok\n"; ' >/tmp/php-fpm-opcache-reset.php; script_filename=/tmp/php-fpm-opcache-reset.php request_method=get cgi-fcgi-bind-connect 127.0.0.1:9000;rm-f/tmp/ php-fpm-opcache-reset.php;
Opcache_invalidate revoking the specified script cache
Boolean opcache_invalidate (String $script [, Boolean $force = FALSE]) The function is to invalidate the byte-code cache for the specified script. If force is not set or is passed FALSE, the script's cache will be invalidated only if the script is modified longer than the corresponding bytecode. Parameter script cache needs to be invalidated for the corresponding scripting path force if the parameter is set to True, the script's cache will be revoked, whether or not it is necessary.
Opcache_invalidate can clean up the cache for single or several scripts.
Summarize
If the code release is a full-volume release, toggle the soft link, you can set Opcache.validate_timestamps=1 and Opcache.validate_timestamps=1 to automatically update the cache on a timed basis.
If the code release overwrites the old directory, you can restart PHP-FPM and use the Opcache_reset function in the script or in the code file to clean up all the caches.
If you can get a list of updated code files, you can use the Opcache_invalidate function to clean up your code, and you can avoid caching that affects other businesses.
PHP7 Opcache Cache Cleanup issues