Automatically flush Redis cache after publishing a WordPress post

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/
 * 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;
}
?>Code language: HTML, XML (xml)

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.

Jan Reilink

Hi, my name is Jan. I am not a hacker, coder, developer or guru. I am merely an application manager / systems administrator, doing my daily thing at Embrace - The Human Cloud. In the past I worked for clidn and Vevida. With over 20 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization. I blog at https://www.saotn.org.

5 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Chris
13/10/2020 19:48

Where does this `Redis` class originate that has the `flushAll()` method mentioned?

15/04/2020 15:51

Hi there,

for me this is not working, ay clue why? Woueld it be possible to flush just the Mainpage (Home) as there are my new Posts showing up.

ems
12/10/2019 19:25

why not use the flushdb instead?