How to Change the Behavior of Cache Purge Plugin

Last Updated -


The Pagely Management plugin is installed on all WordPress sites hosted with Pagely.

By default, it actively listens for WordPress hooks when certain actions such as saving a post are triggered from the /wp-admin/. When an applicable even occurs, it tells the caching layer to purge the cache for the site. In practice, this means that visitors are always able to get the most recent version of the content they're viewing.

An unfortunate side effect of this is that excessive cache purging on too many pages can degrade server performance. After getting the PURGE request, the purge worker on the server has to then scan the disk on the filesystem to see if it has any matches for whatever page-cache exists, being a pretty expensive operation to do. Every PURGE request is essentially invalidating the cache for amount of pages (the default TTL of cached html content is ~ 30 minutes).

Disabling the cache purge can be done numerous ways. You can disable all or part of the cache PURGE requests that occur by using various hooks inside of the Pagely Management plugin.

Pagely Management Cache-Related Hooks

Changing Existing Cache Purge Actions

  • pagely_cache_purge_path: Occurs when a cache purge occurs, and provides the page path.

Triggering a Cache Purge

  • pagely_purge_all: Purges everything from the cache.
  • pagely_purge_comment: Purges the cache for a post, based on a comment ID.
  • pagely_purge_post: Purges a post from the cache.
  • pagely_purge_common: Purges the front page, feeds, and the contents of $PAGELY_CACHE_PURGE_ALWAYS.
  • pagely_cache_purge_after: Callback function utilized after an action is taken.

Variables

You can set a variable called PAGELY_CACHE_PURGE_ALWAYS that specifies endpoints that you want to be purged any time a cache purge action takes place. This can be thrown into an MU plugin:

 $PAGELY_CACHE_PURGE_ALWAYS = [ '/notifications/' ];

Disabling Cache Purge Actions

The pagely_cache_purge_path filter hook can be used for changing cache purge behavior. For more information on how this is used, see examples below.

Disable All Cache Purging When Saving WordPress Posts

<?php
// disable cache purge (wp-content/mu-plugins/custom-stop-site-cache-purging.php)
add_filter('pagely_cache_purge_path', function($url) {

// to disable purging "return null" from the filter - useful during batch imports, etc)
return null;
});

Disable Purging for the Home Page

Similar to previous example but for the homepage only:

// disable cache purge on comments from happening on homepage (wp-content/mu-plugins/custom-stop-site-cache-purging.php)

<?php
add_filter('pagely_cache_purge_path', function($url) {
// Only stop homepage from purging.
if ($url == "/") {
return null;
} else {
return $url;
}
});

Forcing a Cache Purge

Purge All Cached Content

if ( class_exists( 'PagelyCachePurge' ) ) {
$purger = new PagelyCachePurge();
$purger->purgeAll();
}

Purge Specific Items

if ( class_exists( 'PagelyCachePurge' ) ) {
$purger = new PagelyCachePurge();

// the pagely-cache-purge.php:purgePath() function is the main hook which checks & stops all subsequent PURGE requests.
$purger->purgePath('/path/to/thing/');
}

Related Topics

Pagely is the Managed WordPress Hosting Platform designed to exceed the needs of media, business, and Enterprise customers alike. We help the world's biggest brands scale WordPress.

Copyright © 2006-2017 Pagely, Inc. All rights reserved.
Pagely® and WordPress® are registered trademarks.

Pagely
Powered by Zendesk