Last active
August 1, 2018 04:20
-
-
Save hans2103/5727924 to your computer and use it in GitHub Desktop.
Script to invalidate Redis cache in Wordpress after adding new blog posts and such.
To be used with https://gist.github.com/hans2103/5657621 where you already defined the connection information to Redis.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Redis Cache invalidation | |
Author: Flynsarmy and modified by Hans2103 | |
This is a Redis Cache invalidation script for Wordpress. | |
http://www.flynsarmy.com/2013/03/how-to-drastically-speed-up-wordpress-with-redis/ | |
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(); | |
$redis->connect(R_SERVER,R_PORT,2.5); | |
$redis->auth(R_PASSWORD); | |
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(); | |
$redis->flushdb(); | |
/*$domain = $_SERVER['HTTP_HOST']; | |
$dkey = md5($domain); | |
if ( $redis->exists($dkey) ) | |
$redis->del($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' ) | |
return; | |
// 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')) ) | |
return; | |
$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