So this class is very simple class inspired by PyroCache. The idea here is to give developers an easy way to implement caching in their plugins/themes even when the user does not have W3 Total Cache or some other plugin enabled.
Github: https://github.com/mikevanwinkle/Simple-WordPress-Database-Caching/
//in your functions.php or plugin loader
include_once(dirname(__FILE__).'/wpcachedb.php');
$GLOBALS['wpdb'] = new WPCacheDB( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
//elsewhere in your code.
global $wpdb;
$query = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}posts WHERE ID = %d ",2);
//by running the cache method, you automatically check for cached results of this query and create a cache if one does not exist
$posts = $wpdb->cache('get_row',$query,'OBJECT','posts');
//Don't forget to clear the cache at the appropriate time
add_action('save_post','my_clearing_function');
function my_clearing_function($post_id) {
WPCacheDB::clear('posts');
}
This example replaces the global instance of $wpdb with the child class. I only recommend this usage when developing a custom site where you know exactly what other plugins are in use. For plugin and theme development you could just as easily do the following:
//in your functions.php or plugin loader
global $dbcache;
include_once(dirname(__FILE__).'/wpcachedb.php');
$dbcache = new WPCacheDB( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
//elsewhere in you code.
global $dbcache;
$query = $dbcache->prepare("SELECT * FROM {$dbcache->prefix}posts WHERE ID = %d ",2);
//by running the cache method, you automatically check for cached results of this query and create a cache if one does not exist
$posts = $dbcache->cache('get_row',$query,'OBJECT','posts');
//Don't forget to clear the cache at the appropriate time
add_action('save_post','my_clearing_function');
function my_clearing_function($post_id) {
WPCacheDB::clear('posts');
}
This example is less intrusive and would ensure that your plugin is playing nice with the other kids in the sandbox.
Recent Comments