<?php
/*
Name: Memcached
Description: Memcached backend for the WP Object Cache. Need PECL:Memcache.
Version: 0.4
URI: http://www.herewithme.fr
Author: Amaury BALMER
Author URI: http://www.herewithme.fr

* Install this file to wp-content/object-cache.php
* If on Windows, restart IIS after installing for best results

Thanks to Ryan Boren for his original memcached code.
*/

// Configure your memcached servers
global $memcached_servers;
$memcached_servers[] = array( 'host' => '127.0.0.1''port' => 11211'persistant' => true );

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() {
    global 
$wp_object_cache;
    return 
$wp_object_cache->close();
}

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''site-options''site-lookup''blog-lookup''blog-details''rss');
    var 
$autoload_groups = array ('options');
    var 
$cache = array ();
    var 
$cache_enabled true;
    var 
$default_expiration 900;

    
// Memcached object
    
var $mc;

    function 
add($id$data$group 'default'$expire 0) {
        
$key $this->key($id$group);

        if ( 
is_resource($data) ) {
            return 
false;
        }

        
$data maybe_serialize($data);

        
$expire = ($expire == 0) ? $this->default_expiration $expire;
        
$result $this->mc->add($key$dataMEMCACHE_COMPRESSED$expire);

        if ( 
false !== $result ) {
            
$this->cache[$key] = $data;
        }

        return 
$result;
    }

    function 
close() {
        
$this->mc->close();
        return 
true;
    }

    function 
delete($id$group 'default') {
        
$key $this->key($id$group);
        
$result $this->mc->delete($key10);

        if ( 
false !== $result ) {
            unset(
$this->cache[$key]);
        }
        return 
$result;
    }

    function 
flush() {
        
$this->mc->flush();
        return 
true;
    }

    function 
get($id$group 'default') {
        
$key $this->key($id$group);

        if ( isset(
$this->cache[$key]) ) {
            
$value $this->cache[$key];
        } else {
            
$value $this->mc->get($key);
        }

        
$value maybe_unserialize($value);

        if ( 
NULL === $value ) {
            
$value false;
        }

        
$this->cache[$key] = $value;

        if ( 
'checkthedatabaseplease' == $value ) {
            
$value false;
        }

        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) {
        
$key $this->key($id$group);

        if ( 
is_resource($data) ) {
            return 
false;
        }

        
$data maybe_serialize($data);

        
$expire = ($expire == 0) ? $this->default_expiration $expire;
        
$result $this->mc->replace($key$dataMEMCACHE_COMPRESSED$expire);

        if ( 
false !== $result ) {
            
$this->cache[$key] = $data;
        }
        return 
$result;
    }

    function 
set($id$data$group 'default'$expire 0) {
        
$key $this->key($id$group);

        if ( isset(
$this->cache[$key]) && ('checkthedatabaseplease' == $this->cache[$key]) ) {
            return 
false;
        }

        if ( 
is_resource($data) ) {
            return 
false;
        }

        
$data maybe_serialize($data);

        
$this->cache[$key] = $data;
        
$expire = ($expire == 0) ? $this->default_expiration $expire;
        
$result $this->mc->set($key$dataMEMCACHE_COMPRESSED$expire);

        return 
$result;
    }

    function 
stats() {
        echo 
"<pre>\n";
            
print_r($this->mc->getExtendedStats());
        echo 
"</pre>\n";

        if ( !empty(
$this->cache) ) {
            echo 
"<pre>\n";
            
print_r($this->cache);
            echo 
"</pre>\n";
        }
    }

    function 
WP_Object_Cache() {
        global 
$memcached_servers;
        
        
// Instance memcache object
        
$this->mc = new Memcache;        
    
        
// Add default server if global var is empty
        
if ( empty($memcached_servers) ) {
            
$memcached_servers[] = array( 'host' => '127.0.0.1''port' => 11211'persistant' => true );
        }
        
        
// Add server
        
foreach ( (array) $memcached_servers as $server ) {
            
$this->mc->addServer$server['host'], $server['port'], $server['persistant'] );
        }
        
        
// Load compression
        
$this->mc->setCompressThreshold(102400.2);
    }
}
?>