Automatically flush Redis cache after publishing a WordPress post

Did you know you can automatically flush opcode caches like Redis when you publishing a post or page in WordPress? Doing so ensures you and your visitors see the newly created content immediately.
Published on Thursday, 4 April 2019

In a previous post I explained that clearing PHP opcode caches before WordPress Updates helps in streamlining the update process. WordPress updates no longer fail because of cached file locations. Did you know you can automatically flush opcode caches like Redis when you publishing a post or page in WordPress? Doing so ensures you and your visitors see the newly created content immediately.

You can use the following PHP script as a WordPress Must Use Plugin. It'll "fire" on the publish_post action, deleting all cache in Redis whenever you publish a post. Herein lies it's disadvantage: all caches are flushed, and not just that of the home page for instance.

Important notes follow below the code.

Flush Redis cache after post publish in WordPress

Save the following PHP code as a plugin in a new file called, for example, flush-redis.php and upload the file to your wp-content/mu-pluginsfolder.

<?php
/**
 * Plugin Name: Flush Redis cache
 * Plugin URI: https://www.saotn.org/posts/automatically-flush-redis-cache-after-publishing-a-wordpress-post
 * Donate URI: https://www.paypal.me/jreilink
 * Description: Flushes PHP Redis cache upon publish_post action. By clearing the opcode cache in memory you immediately can see a new or changed post.
 * Network: True
 * Version: 1.2
 * Author: Jan Reilink
 * Author URI: https://www.saotn.org
 * License: GPLv2
 */

require_once( ABSPATH .'/wp-config.php' );
function manual_clear_redis_cache() {
  if( class_exists( 'Redis' ) ) {
    $r = new Redis();
    if ($r->connect( WP_REDIS_PATH, 0 )) {
      if( false === $r->flushAll() ) {
        if( WP_DEBUG === true ) {
          error_log( 'After post publish: Flushing PHP Redis failed.' );
        }
        return false;
      }
      else {
        if( WP_DEBUG === true ) {
          error_log( 'After post publish: PHP Redis flushed succesfully.' );
        }
        return true;
      }
    }
    else {
      if( WP_DEBUG === true ) {
        error_log( 'Could not connect to PHP Redis.' );
      }
    }
  }
}
add_action( 'publish_post', 'manual_clear_redis_cache', 10, 2 );

// frc = flush redis cache
add_filter( 'plugin_row_meta', 'frc_plugin_row_meta', 10, 2 );
function frc_plugin_row_meta($links, $file) {
  if ( !preg_match('/flush-redis.php$/', $file ) ) {
    return $links;
  }

  $links[] = sprintf(
    '<a target="_blank" href="https://paypal.me/jreilink" title="Donate to Jan Reilink / Sysadmins of the North">%s</a>',
    __( 'Donate' )
  );
  return $links;
}
?>

Important notes and caveats

  1. this must use plugin to flush Redis cache works perfectly in my situation, it may not in yours.
  2. the plugin relies on a defined value WP_REDIS_PATH, in my case a Unix file-socket. Other Redis opcode cache plugins, like Redis Object Cache, require these connections parameters in wp-config.php, I found it easy to reuse it. If you're not using a plugin like Redis Object Cache, then add the connection parameters to your wp-config.php anyway, or you may need to change the connect() function.
  3. all cached data in Redis is deleted using a flushAll()function.
  4. success and failure are logged to WP_DEBUG_LOG, if WP_DEBUG is enabled. Otherwise the plugin fails or succeeds silently.
  5. other plugins may or may not have different functionality to flush Redis caches.

Please let me know what you think of this little Redis cache busting plugin. If you're using wp-redis by Pantheon, they offer you two alternative cache flushing solutions on their Wiki.