All pastes #954822 Raw Edit

Database-Caching in WP

public php v1 · immutable
#954822 ·published 2008-03-24 03:27 UTC
rendered paste body
<?php/*Name: MySQL-cache for WordPressDescription: MySQL backend for the WP Object Cache.Version: 0.6URI: http://neosmart.net/dl.php?id=15Author: Mahmoud Al-QudsiAuthor URI: http://neosmart.net/blog/* Install this file to wp-content/object-cache.phpThanks to Ryan Boren for his original memcached code.Licensed according to the GPLv2.*//*//First-time installationif (isset($_GET['install'])){	$wpcache = new WP_Object_Cache();	die($wpcache->install());}*/define("INSTALLED", true);function wp_cache_add($key, $data, $flag = '', $expire = 0){	return wp_cache_set($key, $data, $flag, $expire);}function wp_cache_close(){	return true;}function wp_cache_delete($id, $flag = ''){	global $wp_object_cache;	return $wp_object_cache->delete($id, $flag);}function wp_cache_flush(){	global $wp_object_cache;	return $wp_object_cache->flush();}function wp_cache_get($id, $flag = ''){	global $wp_object_cache;	return $wp_object_cache->get($id, $flag);}function wp_cache_init(){	global $wp_object_cache;	$wp_object_cache = new WP_Object_Cache();}function wp_cache_replace($key, $data, $flag = '', $expire = 0){	return wp_cache_set($key, $data, $flag, $expire);}function wp_cache_set($key, $data, $flag = '', $expire = 0){	global $wp_object_cache;	$data = unserialize(serialize($data));	return $wp_object_cache->set($key, $data, $flag, $expire);}class WP_Object_Cache {	var $global_groups = array ('users', 'userlogins', 'usermeta');	var $cache = array();		var $table;	function delete($id, $group = 'default')	{		global $wpdb;		$key = $this->key($id, $group);				$query = "DELETE * FROM {$this->table} WHERE `key`='$key'";		$wpdb->query($query);				unset($this->cache[$key]);				return true;	}	function flush()	{		global $wpdb;				$query = "DELETE * FROM {$this->table}";		$wpdb->query($query);				return true;	}	function get($id, $group = 'default')	{		global $wpdb;				$key = $this->key($id, $group);		if ( isset($this->cache[$key]) )			$value = $this->cache[$key];		else		{			$query = "SELECT `value` FROM {$this->table} WHERE `key`='$id'";			$value = $wpdb->get_var($query);		}				$value = base64_decode(unserialize($value));print_r($value);		if ( NULL === $value )			$value = false;		$this->cache[$key] = $value;		return $value;	}	function set($id, $data, $group = 'default', $expire = 0)	{		global $wpdb;		$key = $this->key($id, $group);		if ( is_resource($data) )			return false;		$data = base64_encode(serialize($data));		$query = "INSERT INTO {$this->table} (`key`,`value`) VALUES ('$id','$data')					ON DUPLICATE KEY UPDATE value='$data';";		$wpdb->query($query);		$this->cache[$key] = $data;		return $true;	}	function key($key, $group)	{		global $blog_id;		if ( empty($group) )				$group = 'default';		if (false !== array_search($group, $this->global_groups))				$prefix = '';		else				$prefix = $blog_id . ':';		return md5(ABSPATH . "$prefix$group:$key");	}	function stats()	{		/*		// Note that this is the total XCache stats, not just WP but also any other apps using XCache var storage		$xcache_info = xcache_info(XC_TYPE_VAR, 0);		echo "<p>\n";		echo "<strong>Cache Hits:</strong> {$xcache_info['hits']}<br/>\n\r";		echo "<strong>Cache Misses:</strong> {$xcache_info['misses']}<br/>\r\n";		echo "</p>\n";		if ( !empty($this->cache) )		{			echo "<pre>\n\r";			print_r($this->cache);			echo "</pre>\n\r";		}		*/	}	function WP_Object_Cache()	{		global $wpdb;		$this->table = $wpdb->prefix . "nst_mysql_cache";		if(INSTALLED!=true)		{			self::install();		}	}		function install()	{		global $wpdb;		$sql="CREATE TABLE IF NOT EXISTS {$this->table} (	`key`		VARCHAR(500)	NOT NULL,	`value`		VARCHAR(500)	DEFAULT NULL,	PRIMARY KEY(`key`))ENGINE=MEMORYPACK_KEYS=1;";		//require_once(ABSPATH . 'wp-admin/includes/upgrade.php');		//dbDelta($sql);				$wpdb->query($sql);		return "The NeoSmart MySQL-based caching extension to the WordPress object-cache has been successfully installed.";	}}?>