PluginProbe
OPcache Reset / 2.2.0
OPcache Reset v2.2.0
trunk 1.0.0 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.3.0 2.4.0 2.4.1 2.4.2 2.4.3
opcache-reset / opcache-reset.php

opcache-reset.php in OPcache Reset 2.2.0, at opcache-reset.php

96 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: OPcache Reset
4 * Plugin URI: http://wordpress.org/plugins/opcache-reset/
5 * Description: Automatic reset of OPcache
6 * Version: 2.2.0
7 * Author: Danila Vershinin
8 * Author URI: https://www.getpagespeed.com/
9 * License: GPL2
10 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
11 */
12
13 // Make sure we don't expose any info if called directly
14 if ( !function_exists( 'add_action' ) ) {
15 echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
16 exit;
17 }
18
19 if ( is_admin() ) {
20 if (! (defined( 'DOING_AJAX' ) && DOING_AJAX)) {
21 // We are in admin mode: notices and such are handled here
22 require_once(__DIR__ . '/opcache-admin.php');
23 }
24 }
25
26 /**
27 * Reset OPCache using different approach depending on the caller context (e.g. cron vs. web)
28 */
29 function gps_opcache_reset() {
30
31 if( ! function_exists('opcache_reset') ) {
32 // Bail if extension is not loaded
33 return;
34 }
35
36
37 if (empty( ini_get( 'opcache.enable' ))) {
38 // Do not try doing anything if OPcache is loaded but disabled
39 return;
40 }
41
42 if ( ! empty( ini_get( 'opcache.restrict_api' ) ) && strpos( __FILE__, ini_get( 'opcache.restrict_api' ) ) !== 0 ) {
43 return;
44 }
45
46 // https://www.getpagespeed.com/server-setup/php/zend-opcache
47 // Follow the principles of reliable file cache clearing from this article
48 $fileCacheDir = ini_get( 'opcache.file_cache' );
49 // Check if file cache is enabled and delete it if enabled
50 if ( $fileCacheDir && is_writable( $fileCacheDir ) ) {
51 // check if we can create subdirectory in the parent directory.
52 // Normally, it's ~/.cache so we can
53 $cacheDir = dirname($fileCacheDir);
54 if (! is_writable($cacheDir)) {
55 $fileCacheDir = null;
56 }
57 }
58
59 if ($fileCacheDir && file_exists($fileCacheDir)) {
60 // move it out of the way to avoid race conditions
61 shell_exec("mv ${fileCacheDir} ${fileCacheDir}.rm");
62 }
63
64 // We are in PHP-FPM context and not file cache only
65 if (php_sapi_name() !== 'cli' && ! ini_get( 'opcache.file_cache_only' )) {
66 opcache_reset();
67 }
68
69 if ($fileCacheDir) {
70 if ( file_exists( "${fileCacheDir}.rm" )) {
71 shell_exec( "rm -rf ${fileCacheDir}.rm" );
72 }
73 // make sure OPcache directory is re-created
74 shell_exec( "mkdir -p ${fileCacheDir}" );
75 }
76
77 // Irrespective of the context, we should attempt to clear the memory-based OPCache, because this script may be called from CLI
78 // and can't be aware of whether PHP-FPM is running with file_cache_only => On
79 // Since the script may be called via cron or PHP-FPM with clear_env settings, the PATH may not be set properly, so we need to set it
80 $username = getenv('USER');
81 $path = "/home/${username}/.local/bin:/usr/bin:/bin";
82 $systemPath = getenv('PATH');
83 if ($systemPath) {
84 $path .= ':' . $systemPath;
85 }
86 $cmd = 'php -d opcache.enable_cli=0 -d opcache.file_cache_only=0 -d opcache.file_cache=/tmp $(which cachetool) opcache:reset';
87 // Run cachetool with the adjusted PATH
88 shell_exec("PATH=${path} ${cmd} 2>&1");
89 }
90
91
92 add_action( 'upgrader_process_complete', 'gps_opcache_reset', PHP_INT_MAX - 1, 2 );
93
94
95
96