Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to invalidate Redis cache in Wordpress after adding new blog posts and such. To be used with where you already defined the connection information to Redis.
Redis Cache invalidation
Author: Flynsarmy and modified by Hans2103
This is a Redis Cache invalidation script for Wordpress.
defined the connection credentials in wp-config.php
Add this piece of code to your theme functions.php or...
when using Roots theme add this as a file to the lib directory and call it from functions.php
define('R_SERVER', '<servername>'); // The server of your Redis database
define('R_PORT', <portnumber>); // The port to your Redis server
define('R_PASSWORD', '<password>'); // ...and password
define('R_PREFIX_KEY', ''); // Redis key prefix
function get_redis()
// Redis connection information
// defined in wp-config.php
$redis = new Redis();
return $redis;
// Chances are you'll have a list of categories on every page.
// So delete all site cache if categories are messed with
add_action('add_category', 'redis_invalidate_all');
add_action('delete_category', 'redis_invalidate_all');
add_action('edit_category', 'redis_invalidate_all');
// Delete all site cache for the current domain
function redis_invalidate_all()
$redis = get_redis();
/*$domain = $_SERVER['HTTP_HOST'];
$dkey = md5($domain);
if ( $redis->exists($dkey) )
// When adding/editing/deleting a post, invalidate post and home pages
add_action('trashed_post', 'redis_invalidate_post');
add_action('save_post', 'redis_invalidate_post');
add_action('publish_post', 'redis_invalidate_post');
add_action('edit_post', 'redis_invalidate_post');
add_action('pre_post_update', 'redis_invalidate_post');
function redis_invalidate_post( $post_id )
// Don't delete cache on auto-save
if ( isset($_POST['action']) && $_POST['action'] == 'autosave' )
// Don't delete cache if we're saving as draft or pending
if ( isset($_POST['save']) && in_array($_POST['save'], array('Save Draft', 'Save as Pending')) )
$redis = get_redis();
$domain = $_SERVER['HTTP_HOST'];
$dkey = md5($domain);
// Invalidate homepage
//$ukey = md5("http://".$_SERVER['HTTP_HOST'].'/');
$ukey = md5(site_url().'/');
if ( $redis->hexists(R_PREFIX_KEY.$ukey, $dkey) )
$redis->hdel(R_PREFIX_KEY.$ukey, $dkey);
// Invalidate post page
$ukey = md5($permalink = get_permalink( $post_id ));
if ( $redis->hexists(R_PREFIX_KEY.$ukey, $dkey) )
$redis->hdel(R_PREFIX_KEY.$ukey, $dkey);
// When adding/editing/deleting a comment, invalidate post and home pages
add_action('comment_closed', 'redis_invalidate_comment');
add_action('comment_post', 'redis_invalidate_comment');
add_action('edit_comment', 'redis_invalidate_comment');
add_action('delete_comment', 'redis_invalidate_comment');
add_action('wp_set_comment_status', 'redis_invalidate_comment');
function redis_invalidate_comment( $comment_id )
$comment = get_comment( $comment_id );
redis_invalidate_post( $comment->comment_post_ID );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.