<?php
/*

Name: eAccelerator Object Cache for WordPress
Description: eAccelerator persistent storage for the WP Object Cache.
Version: 1.1
URI: 
Author: Mike Beggs
Author URI: http://murmatrons.armadillo.homeip.net/

Based on Ryan Boren's memcached implementation.

* Install to to wp-content/object-cache.php

* Chances are that on a single server blog installation with MySQL query caching
  and nothing beyond the core WordPress objects being cached, using this will
  probably make page generation slower than not using it. Go figure.
  
  Choose wisely.

*/

if(!function_exists('eaccelerator_put')) {
    include_once(
ABSPATH.WPINC.'/cache.php'); // Fall back to default WordPress object cache
} else {

    @
define('OBCACHE_NON_PERSISTENT_TTL', -1); // -1=Persists only for this script execution
        
    
@define('OBCACHE_DEFAULT_TTL'600);
        

    function 
wp_cache_close() {
    
        return 
true;
    }

    function 
wp_cache_delete($id$group '') {
    
        global 
$wp_object_cache;
        return 
$wp_object_cache->delete($id$group);
    }

    function 
wp_cache_flush() {
    
        global 
$wp_object_cache;
        return 
$wp_object_cache->flush();
    }

    function 
wp_cache_get($id$group '') {
    
        global 
$wp_object_cache;
        return 
$wp_object_cache->get($id$group);
    }

    function 
wp_cache_init() {
    
        global 
$wp_object_cache;
        
$wp_object_cache = new WP_Object_Cache();
    
    }

    function 
wp_cache_replace($key$data$group ''$expireOBCACHE_DEFAULT_TTL) {
        global 
$wp_object_cache;
        return 
$wp_object_cache->set($key$data$group$expire);
    }

    function 
wp_cache_add($key$data$group ''$expireOBCACHE_DEFAULT_TTL) {
        global 
$wp_object_cache;
        return 
$wp_object_cache->set($key$data$group$expire);
    }

    function 
wp_cache_set($key$data$group ''$expireOBCACHE_DEFAULT_TTL) {
        global 
$wp_object_cache;
        return 
$wp_object_cache->set($key$data$group$expire);
    }

    function 
wp_cache_add_global_groups($groups) {
        global 
$wp_object_cache;
        
$wp_object_cache->add_global_groups($groups);
    }

    function 
wp_cache_add_non_persistent_groups($groups) {
        global 
$wp_object_cache;
        
$wp_object_cache->add_non_persistent_groups($groups);
    }

    class 
WP_Object_Cache {

        var 
$global_groups = array('users''userlogins''usermeta''site-options''site-lookup''blog-lookup''blog-details''rss');
        var 
$non_persistent_groups = array();
        var 
$autoload_groups = array();
        
        
//var $non_persistent_cache = array();
        
        // $enabled & $expiration_time added to satisfy the needs of wpmu-sitewide-feed plugin
        // which wants to access these class members directly even though they might not exist
        // Presumably a throw-back to a wp_object_cache of yester-year
        
var $enabled true;    
        var 
$expiration_time OBCACHE_DEFAULT_TTL;
        
        function 
add_global_groups($groups) {

            if(!
is_array($groups)) $groups = (array)$groups;

            
$this->global_groups=array_merge($this->global_groups$groups);
            
$this->global_groups=array_unique($this->global_groups);
        }
        
        function 
add_non_persistent_groups($groups) {

            if(!
is_array($groups)) $groups = (array)$groups;

            
$this->non_persistent_groups=array_merge($this->non_persistent_groups$groups);
            
$this->non_persistent_groups=array_unique($this->non_persistent_groups);
        }

        function 
delete($id$group 'default') {
        
            global 
$blog_id;
        
            
$key $this->key($id$group);
            
            
//unset($this->non_persistent_cache[$blog_id][$group][$id]);
            
            
return eaccelerator_rm($key);
        }

        function 
flush() {
            
            global 
$blog_id;
        
            
$keylist eaccelerator_list_keys();
            
            foreach (
$keylist as $key) {
                
// Trim leading ":" to work around list_keys namespace bug in eAcc.
                // This will still work when bug is fixed
                
$key['name'] = ltrim($key['name'], ':'); 
                if(
strstr($key['name'], ':wp-obcache:')) {
                    
eaccelerator_rm($key['name']);
                }
            }
            
            
//unset($this->non_persistent_cache[$blog_id]);
            
            
return true;
        }

        function 
get($id$group 'default') {
        
            global 
$blog_id;
        
            
$key $this->key($id$group);
            
            
//if(isset($this->non_persistent_cache[$blog_id][$group][$id]))
            //        return $this->non_persistent_cache[$blog_id][$group][$id];
            
            
$value unserialize(eaccelerator_get($key));

            if(
NULL === $value$value false;

            return 
$value;
        }
        
        function 
add($id$data$group 'default'$expire OBCACHE_DEFAULT_TTL) {
        
            return 
$this->set($id$data$group$expire);
        }
        function 
replace($id$data$group 'default'$expire OBCACHE_DEFAULT_TTL) {
        
            return 
$this->set($id$data$group$expire);
        }         
        function 
set($id$data$group 'default'$expire OBCACHE_DEFAULT_TTL) {
        
            global 
$blog_id;
            
            if(
is_resource($data))    return false;

            if(
in_array($group$this->non_persistent_groups)) $expire=OBCACHE_NON_PERSISTENT_TTL;

            
$key $this->key($id$group);

            
//$this->non_persistent_cache[$blog_id][$group][$id] = $data;

            
if($expire 0) return true;
            
            return 
eaccelerator_put($keyserialize($data), $expire);
                            
        }

        function 
key($key$group)    {
        
            global 
$blog_id;

            if (empty(
$group))
                
$group 'default';

            if(
in_array($group$this->global_groups))
                
$prefix 'wp-obcache:';
            else
                
$prefix $blog_id.':wp-obcache:';

            
//return md5(ABSPATH."$prefix$group:$key"); // Useful for debug
            
return "$prefix$group:".md5($key);
        }

        function 
stats() {
        
            echo 
"<p>Stats not implemented</p>\n";

        }

        function 
WP_Object_Cache() {}
        
    }
    
    
wp_cache_init();
}