Skip to content

Instantly share code, notes, and snippets.

@dennyhalim
Created September 6, 2009 07:23
Show Gist options
  • Save dennyhalim/181699 to your computer and use it in GitHub Desktop.
Save dennyhalim/181699 to your computer and use it in GitHub Desktop.
<?php
/*
Name: APC Cache
Description: APC backend for the WP Object Cache.
Version: 0.2.1
URI: http://txfx.net/code/wordpress/apc-cache/
Author: Mark Jaquith
Install this file to wp-content/object-cache.php
Big thanks to Ryan Boren (http://boren.nu/) whose Memcached backend plugin
made the writing of this plugin a 5 minute operation.
Added changes (serialization) to make caching work with multi-level arrays
by Eric Byers (ericbyers.com)
Notes by Quentin Gouedard ( http://quentin.unblog.fr/ )
Added a minimum expiry time.
Fixed the case of blog creation in WPMU.
Made the flush flush only cached variables, and not every cached script.
Adapted global groups to WPMU
Fixed key creation a bit
*/
// gracefully revert to default cache if APC is not installed
if ( !function_exists('apc_store') ) {
include_once(ABSPATH . WPINC . '/cache.php');
} else {
function wp_cache_add($key, $data, $flag = '', $expire = 0) {
global $wp_object_cache;
return $wp_object_cache->add($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) {
global $wp_object_cache;
return $wp_object_cache->replace($key, $data, $flag, $expire);
}
function wp_cache_set($key, $data, $flag = '', $expire = 0) {
global $wp_object_cache;
if ( defined('WP_INSTALLING') == false )
return $wp_object_cache->set($key, $data, $flag, $expire);
else
return true;
}
class WP_Object_Cache {
var $global_groups = array ('users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details');
var $cache = array ();
var $minexpiry = 900;
function add($id, $data, $group = 'default', $expire = 0) {
return $this->set($id, $data, $group, $expire);
}
function delete($id, $group = 'default') {
$key = $this->key($id, $group);
$result = apc_delete($key);
if ( false !== $result )
unset($this->cache[$key]);
return $result;
}
function flush() {
apc_clear_cache('user');
return true;
}
function get($id, $group = 'default') {
$key = $this->key($id, $group);
if (isset($this->cache[$key])) {
$value = $this->cache[$key];
} else {
$value = apc_fetch($key);
if ($value !== NULL) {
$value = unserialize($value);
}
}
/* echo "Cache key: $key<br/>";
echo 'Cache value:<br/>';
var_dump($value);
echo '<br/>';*/
if ( NULL === $value ) {
$value = false;
}
$this->cache[$key] = $value;
return $value;
}
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 . '-';
if( '' != $key )
$key = '-' . $key;
return "$prefix$group$key";
}
function replace($id, $data, $group = 'default', $expire = 0) {
return $this->set($id, $data, $group, $expire);
}
function set($id, $data, $group = 'default', $expire = 0) {
$key = $this->key($id, $group);
// quentin set a ttl
if($expire==0) $expire=$this->minexpiry;
$result = apc_store($key, serialize($data), $expire);
if ( false !== $result ) {
$this->cache[$key] = $data;
}
return $result;
}
function stats() {
$apc_info = apc_cache_info();
echo "<p>\n";
echo "<strong>Cache Hits:</strong> {$apc_info['num_hits']}<br/>\n";
echo "<strong>Cache Misses:</strong> {$apc_info['num_misses']}<br/>\n";
echo "</p>\n";
if ( ! empty($this->cache) ) {
echo "<pre>\n";
print_r($this->cache);
echo "</pre>\n";
}
}
function WP_Object_Cache() {
// Nothing here
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment