PluginProbe
OPcache Reset / 2.1.8
OPcache Reset v2.1.8
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.1.8, at opcache-reset.php

98 lines 3.3 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.1.8
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
50 // File cache dir may be set for CLI only, so let's check it
51 if ( ! $fileCacheDir ) {
52 $cmd_ini_get = "echo ini_get('opcache.file_cache');";
53 // Use the PHP wrapper in local bin, see how we like to to put ~./local/bin/php as a specific PHP with different file OPCache settings
54 // https://www.getpagespeed.com/server-setup/php/faster-php-cli-and-cron-jobs
55 $cmd_ini_get = 'PATH=$HOME/.local/bin:$PATH php -r ' . '"' . $cmd_ini_get . '"';
56 $fileCacheDir = trim(shell_exec($cmd_ini_get));
57 }
58
59 // Check if file cache is enabled and delete it if enabled
60 if ( $fileCacheDir && is_writable( $fileCacheDir ) ) {
61 // check if we can create subdirectory in the parent directory.
62 // Normally, it's ~/.cache so we can
63 $cacheDir = dirname($fileCacheDir);
64 if (! is_writable($cacheDir)) {
65 $fileCacheDir = null;
66 }
67 }
68
69 if ($fileCacheDir && file_exists($fileCacheDir)) {
70 // move it out of the way to avoid race conditions
71 shell_exec("mv ${fileCacheDir} ${fileCacheDir}.rm");
72 }
73
74 // We are in PHP-FPM context and not file cache only
75 if (php_sapi_name() !== 'cli' && ! ini_get( 'opcache.file_cache_only' )) {
76 opcache_reset();
77 }
78
79 if ($fileCacheDir) {
80 // if file cache only, do not use opcache:reset as it cannot clear file caches and yield error in such case
81 if (! ini_get( 'opcache.file_cache_only' )) {
82 shell_exec('php -d opcache.enable_cli=0 -d opcache.file_cache=/tmp $(which cachetool) opcache:reset');
83 }
84 if ( file_exists( "${fileCacheDir}.rm" )) {
85 shell_exec( "rm -rf ${fileCacheDir}.rm" );
86 }
87 // make sure OPcache directory is re-created
88 shell_exec( "mkdir -p ${fileCacheDir}" );
89 }
90
91 }
92
93
94 add_action( 'upgrader_process_complete', 'gps_opcache_reset', PHP_INT_MAX - 1, 2 );
95
96
97
98