Skip to content

Instantly share code, notes, and snippets.

@Digiover
Last active November 12, 2018 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Digiover/42070d49a6f8caa89d3161eccec426e9 to your computer and use it in GitHub Desktop.
Save Digiover/42070d49a6f8caa89d3161eccec426e9 to your computer and use it in GitHub Desktop.
Flush PHP memcacheD cache in WordPress - Flushes PHP memcached cache upon publish_post action. By clearing the opcode cache in memory you immediately can see a new or changed post
<?php
/**
* Plugin Name: Flush MemcacheD cache
* Plugin URI: https://www.saotn.org/
* Donate URI: https://www.paypal.me/jreilink
* Description: Flushes PHP memcached cache upon publish_post action. By clearing the opcode cache in memory you immediately can see a new or changed post. Use as a Must-Use Plugin.
* 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_memcached_cache() {
if( class_exists( 'Memcached' ) ) {
/* $memcached_servers comes from the plugins:
* https://wordpress.org/plugins/memcached/, or
* https://wordpress.org/plugins/memcached-is-your-friend/
*
* change this if you're using something else, or add your Unix socket path like:
* https://gist.github.com/Digiover/f3a30849f621cdb97e3346b300711e74
*/
global $memcached_servers;
if( isset($memcached_servers)) {
list( $socket, $port ) = explode( ":", $memcached_servers["default"][0] );
$m = new Memcached();
$m->addServer( $socket, $port);
if ( false === $m->flush() ) {
// result is logged to WP Debug log
error_log( 'After post publish: Clearing Memcached failed.' );
return false;
}
else {
// result is logged to WP Debug log
error_log( 'After post publish: Memcached flushed' );
return true;
}
}
}
}
add_action( 'publish_post', 'manual_clear_memcached_cache', 10, 2 );
// fmd = flush memcached
add_filter( 'plugin_row_meta', 'fmd_plugin_row_meta', 10, 2 );
function fmd_plugin_row_meta($links, $file) {
if ( !preg_match('/saotn-flush-memcached.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;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment