<?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)
*/



// 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;

        return 
$wp_object_cache->set($key$data$flag$expire);
}

class 
WP_Object_Cache {
        var 
$global_groups = array ('users''userlogins''usermeta');
        var 
$cache = array ();

        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();
                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 ':';

                return 
md5(ABSPATH "$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);
                
$result apc_store($keyserialize($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
        
}
}       
}
?>