Skip to content

Instantly share code, notes, and snippets.

@bjornjohansen
Created November 4, 2015 10:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjornjohansen/eefbe3bfa4f5f0270a3e to your computer and use it in GitHub Desktop.
Save bjornjohansen/eefbe3bfa4f5f0270a3e to your computer and use it in GitHub Desktop.
Loading the Optimal WordPress Object Cache Implementation (modified)
<?php
/**
* Based on an idea and code by webdev studios: https://webdevstudios.com/2015/11/03/loading-the-optimal-wordpress-object-cache-implementation-in-your-production-staging-and-local-development-environments/
*/
$plugin_dir = ( defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins' );
// If Redis exists and redis server is configured,
if ( class_exists( 'Redis' ) && isset( $GLOBALS['redis_server'] ) ) {
// Then load our object cache plugin (if available).
if ( file_exists( $plugin_dir . '/wp-redis/object-cache.php' ) ) {
require_once $plugin_dir . '/wp-redis/object-cache.php';
}
}
// Fallback to check for memcached if redis server isn’t available
elseif ( class_exists( 'Memcache' ) && isset( $GLOBALS['memcached_servers'] ) ) {
// Then load our object cache plugin (if available).
if ( file_exists( $plugin_dir . '/memcached/object-cache.php' ) ) {
require_once $plugin_dir . '/memcached/object-cache.php';
}
}
/**
* If we can't use the object-cache, we need some trickery
* for WP to believe we aren't actually using an object-cache
* (which it assumes since we have this file)
*/
else {
// Helper/callback.
function set_wp_using_ext_object_cache_to_false() {
wp_using_ext_object_cache( false );
}
// Set to false now.
// (After this file loads, WP resets to true.)
set_wp_using_ext_object_cache_to_false();
// Include the built-in WP object-caching.
require_once( ABSPATH . WPINC . '/cache.php' );
// Hook in to reset to false,
add_action(
'muplugins_loaded', // To the earliest hook,
'set_wp_using_ext_object_cache_to_false',
-9999 // At a super early priority.
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment