Skip to content

Commit

Permalink
Invalidate advanced-cache.php only, instead of resetting the entire O…
Browse files Browse the repository at this point in the history
…pcache by default.
  • Loading branch information
jaswsinc committed Dec 15, 2015
1 parent 3500806 commit fd93416
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/includes/classes/MenuPageOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ public function __construct()
if ($this->plugin->functionIsPossible('opcache_reset')) {
echo ' <hr />'."\n";
echo ' <h3>'.__('Clear the <a href="http://zencache.com/r/php-opcache/" target="_blank">PHP OPCache</a> Too?', SLUG_TD).'</h3>'."\n";
echo ' <p>'.sprintf(__('If you clear the cache manually, do you want %1$s to clear the PHP OPCache too? This is highly recommended, so that all PHP files in the server\'s opcode cache are purged from memory also.', SLUG_TD), esc_html(NAME)).'</p>'."\n";
echo ' <p>'.sprintf(__('If you clear the cache manually, do you want %1$s to clear the PHP OPCache too? This is not necessary, but if you want a truly clean start, this will clear all PHP files in the server\'s opcode cache also. Note: If you don\'t already know what the PHP OPcache is, it is suggested that you leave this disabled. It really is not necessary. This is just an added feature for advanced users.', SLUG_TD), esc_html(NAME)).'</p>'."\n";
echo ' <p><select name="'.esc_attr(GLOBAL_NS).'[saveOptions][cache_clear_opcache_enable]" class="-no-if-enabled">'."\n";
echo ' <option value="1"'.selected($this->plugin->options['cache_clear_opcache_enable'], '1', false).'>'.__('Yes, if the PHP OPCache extension is enabled, also clear the PHP opcode cache.', SLUG_TD).'</option>'."\n";
echo ' <option value="0"'.selected($this->plugin->options['cache_clear_opcache_enable'], '0', false).'>'.__('No, I don\'t use the PHP OPCache extension; or, I don\'t want the opcode cache cleared.', SLUG_TD).'</option>'."\n";
echo ' <option value="1"'.selected($this->plugin->options['cache_clear_opcache_enable'], '1', false).'>'.__('Yes, if the PHP OPCache extension is enabled, also clear the entire PHP opcode cache.', SLUG_TD).'</option>'."\n";
echo ' </select></p>'."\n";
}
if ($this->plugin->functionIsPossible('s2clean')) {
Expand Down Expand Up @@ -557,7 +557,6 @@ public function __construct()
echo ' <p class="info">'.__('<strong>Note:</strong> please remember that your entries here should be formatted as a line-delimited list; e.g., one exclusion pattern per line.', SLUG_TD).'</p>'."\n";
echo ' </div>'."\n";


echo '</div>'."\n";

/* ----------------------------------------------------------------------------------------- */
Expand Down Expand Up @@ -667,7 +666,6 @@ public function __construct()

/* ----------------------------------------------------------------------------------------- */


echo '<div class="plugin-menu-page-panel">'."\n";

echo ' <a href="#" class="plugin-menu-page-panel-heading">'."\n";
Expand Down
17 changes: 9 additions & 8 deletions src/includes/classes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@ public function setup()
$this->default_options = array(
/* Core/systematic plugin options. */

'version' => VERSION,
'welcomed' => '0', // `0|1` welcomed yet?
'crons_setup' => '0', // A timestamp when last set up.
'version' => VERSION,
'welcomed' => '0', // `0|1` welcomed yet?
'crons_setup' => '0', // A timestamp when last set up.
'crons_setup_on_namespace' => '', // The namespace on which they were set up.
'crons_setup_with_cache_cleanup_schedule' => '', // The cleanup schedule selected by site owner during last setup.
'crons_setup_on_wp_with_schedules' => '', // A sha1 hash of `wp_get_schedules()`
Expand Down Expand Up @@ -286,7 +287,7 @@ public function setup()
'cache_clear_admin_bar_roles_caps' => '', // Comma-delimited list of roles/caps.

'cache_clear_cdn_enable' => '0', // `0|1`.
'cache_clear_opcache_enable' => '1', // `0|1`.
'cache_clear_opcache_enable' => '0', // `0|1`.
'cache_clear_s2clean_enable' => '0', // `0|1`.
'cache_clear_eval_code' => '', // PHP code.
'cache_clear_urls' => '', // Line-delimited list of URLs.
Expand Down Expand Up @@ -318,10 +319,10 @@ public function setup()

/* Related to exclusions. */

'exclude_uris' => '', // Empty string or line-delimited patterns.
'exclude_uris' => '', // Empty string or line-delimited patterns.
'exclude_client_side_uris' => '', // Line-delimited list of URIs.
'exclude_refs' => '', // Empty string or line-delimited patterns.
'exclude_agents' => 'w3c_validator', // Empty string or line-delimited patterns.
'exclude_refs' => '', // Empty string or line-delimited patterns.
'exclude_agents' => 'w3c_validator', // Empty string or line-delimited patterns.

/* Related to version salt. */

Expand Down Expand Up @@ -363,7 +364,7 @@ public function setup()
'cdn_invalidation_var' => 'iv', // A query string variable name.
'cdn_invalidation_counter' => '1', // Current version counter.

'cdn_over_ssl' => '0', // `0|1`; enable SSL compat?
'cdn_over_ssl' => '0', // `0|1`; enable SSL compat?
'cdn_when_logged_in' => '0', // `0|1`; enable when logged in?
'cdn_whitelisted_extensions' => '', // Whitelisted extensions.
Expand Down
6 changes: 6 additions & 0 deletions src/includes/closures/Plugin/InstallUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@
}
$self->cacheUnlock($cache_lock); // Release.

$self->clearAcDropinFromOpcacheByForce();

return true;
};

Expand Down Expand Up @@ -408,6 +410,8 @@
if (file_put_contents($advanced_cache_file, '') !== 0) {
return false; // Failure.
}
$self->clearAcDropinFromOpcacheByForce();

return true;
};

Expand Down Expand Up @@ -435,6 +439,8 @@
return false; // Not possible; or outright failure.
}
}
$self->clearAcDropinFromOpcacheByForce();

return true; // Deletion success.
};

Expand Down
24 changes: 20 additions & 4 deletions src/includes/closures/Plugin/WcpOpcacheUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
*
* @param bool $manually True if wiping is done manually.
* @param boolean $maybe Defaults to a true value.
* @param array $files Optional; wipe only specific files?
*
* @return integer Total keys wiped.
*/
$self->wipeOpcache = function ($manually = false, $maybe = true) use ($self) {
$self->wipeOpcache = function ($manually = false, $maybe = true, $files = array()) use ($self) {
$counter = 0; // Initialize counter.

if ($maybe && !$self->options['cache_clear_opcache_enable']) {
Expand All @@ -27,10 +28,14 @@
if (empty($status->opcache_enabled)) {
return $counter; // Not necessary.
}
if (!isset($status->opcache_statistics->num_cached_keys)) {
if (empty($status->opcache_statistics->num_cached_keys)) {
return $counter; // Not possible.
}
if (opcache_reset()) { // True if a reset occurs.
if ($files) { // Specific files?
foreach ($files as $_file) {
$counter += (int) opcache_invalidate($_file, true);
} // unset($_file); // Housekeeping.
} elseif (opcache_reset()) { // True if a reset occurs.
$counter += $status->opcache_statistics->num_cached_keys;
}
return $counter;
Expand All @@ -41,7 +46,7 @@
*
* @since 151002 Adding OPCache support.
*
* @param bool $manually True if wiping is done manually.
* @param bool $manually True if clearing is done manually.
* @param boolean $maybe Defaults to a true value.
*
* @return integer Total keys cleared.
Expand All @@ -52,4 +57,15 @@
}
return 0; // Not applicable.
};

/*
* Clear AC class file from Opcache (by force).
*
* @since 151215 Adding OPCache support.
*
* @return integer Total keys cleared.
*/
$self->clearAcDropinFromOpcacheByForce = function () use ($self) {
return $self->wipeOpcache(false, false, array(WP_CONTENT_DIR.'/advanced-cache.php'));
};
/*[/pro]*/

1 comment on commit fd93416

@jaswrks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.