<?php
/**
 * This library is a simple memcache PHP5 library that will
 * interact with a server running memcached.
 *
 * @package Memcache
 * @author Ben Maynard
 * @url http://blog.visionsource.og
 **/

class CacheManager
{
    
/**
     * @var Memcache $_memCached The memcached object
     * @access private
     */
    
private $_memCache;

    
/**
     * @var bool $_doDebug Flag to tell the library if we are debug is enabled or not
     * @access private
     */
    
private $_doDebug;

    
/**
     * @var array $_debug An array that stores all the debug information
     * @access private
     */
    
private $_debug;

    
/**
     * @var string $_prefix The prefix to add to the key.This is handy if you have multiple sites that are running on the same server and may share the same key
     * @access public
     */
    
private $_prefix;

    
/**
     * @var bool $_md5Key Set to true if you wish to md5 the key
     * @access private
     */
    
private $_md5Key;

    
/**
     * Class Constructor
     * 
     * @access public
     * @return none
     */

    
public function __construct$prefix ""$md5Key true$debug false )
    {
        
$this->_memCache = new Memcache( );
        
$this->_doDebug $debug;
        
$this->_prefix $prefix;
        
$this->_md5Key $md5Key;
        
        if ( 
$this->_doDebug === true )
        {
            
$this->_debug['set'] = array( );
            
$this->_debug['replace'] = array( );
            
$this->_debug['get'] = array( );
            
$this->_debug['delete'] = array( );
            
$this->_debug['errors'] = array( );
        }
    }

    
/**
     * Get the instance of the library
     *
     * @access public
     * @static
     * @return CacheManager
     */

    
public static function &getInstance$prefix ""$md5Key true$debug false )
    {
        static 
$_instance;

        if ( 
is_null$_instance ) )
        {
            
$_instance = array( );
        }

        
$options $prefix . (string)$md5Key . (string)$debug;

        if ( !isset( 
$_instance[$options] ) )
        {
            
$_instance[$options] = new CacheManager($prefix$md5Key$debug);
        }

        return 
$_instance[$options];
    }

    
/**
     * Add a memcached server
     *
     * @param string $serverAddress The memcache server address
     * @param int $serverPort The memcache sever port
     * @param bool $persistent Controls the use of a persistent connection
     * @param int $weight Number of buckets to create for this server which in turn control its probability of it being selected. The probability is relative to the total weight of all servers. 
     * @param int $timeout Value in seconds which will be used for connecting to the daemon. Think twice before changing the default value of 1 second - you can lose all the advantages of caching if your connection is too slow. 
     * @param int $retryInterval Controls how often a failed server will be retried, the default value is 15 seconds. Setting this parameter to -1 disables automatic retry.
     * @param bool $status Controls if the server should be flagged as online. Setting this parameter to FALSE and retry_interval  to -1 allows a failed server to be kept in the pool so as not to affect the key distribution algorithm.
     * @access public
     * @return bool
     */

    
public function addServer$serverAddress$serverPort 11211$persistent true$weight 1$timeout 1$retryInterval 15$status true )
    {
        return 
$this->_memCache->addServer$serverAddress$serverPort$persistent$weight$timeout$retryInterval$status, array( &$this'_callback_memcache_failure' ) );
    }

    
/**
     * Callback function if the connection to the server fails
     *
     * @access public
     */

    
public function _callback_memcache_failure$host$port )
    {
        if ( 
$this->_doDebug === true )
        {
            
$this->_debug['errors'] = 'Connection failed for: ' $host ':' $port;
        }
    }

    
/**
     * Set a value on the memcache server
     * 
     * @param string $key The key to set
     * @param string $value The value to cache
     * @param int $flag Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
     * @param int $ttl How long the cache item to live
     * @access public
     * @return bool
     */
    
    
public function set$key$value$flag 0$ttl 3600 )
    {
        
$key $key $this->_prefix;

        if ( 
$this->_md5Key === true )
        {
            
$key md5$key );
        }

        if ( 
$this->_doDebug === true )
        {
            
$this->_debug['set'][$key] = $value;
        }

        return 
$this->_memCache->set$key$value$flag$ttl );
    }

    
/**
     * Replace an value of an existing item
     *
     * @param string $key The key to set
     * @param string $value The value to cache
     * @param int $flag Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).
     * @param int $ttl How long the cache item to live
     * @access public
     * @return bool
     */

    
public function replace$key$value$flag 0$ttl 3600 )
    {
        
$key $key $this->_prefix;

        if ( 
$this->_md5Key === true )
        {
            
$key md5$key );
        }

        if ( 
$this->_doDebug === true )
        {
            
$this->_debug['replace'][$key] = $value;
        }

        return 
$this->_memCache->replace$key$value$flag$ttl );
    }

    
/**
     * Retrive the value of the requested cached item
     *
     * @param string $key The key of the cache item to retrieve
     * @access public
     * @return mixed
     */
    
    
public function get$key )
    {
        
$key $key $this->_prefix;

        if ( 
$this->_md5Key === true )
        {
            
$key md5$key );
        }

        
$cachedOutput $this->_memCache->get$key );

        if ( 
$this->_doDebug === true )
        {
            
$this->_debug['get'][$key] = $cachedOutput;
        }

        return 
$cachedOutput;
    }

    
/**
     * Delete an item out of the cache server
     *
     * @param string $key The key of the cached item
     * @access public
     * @return bool
     */

    
public function delete$key )
    {
        
$key $key $this->_prefix;

        if ( 
$this->_md5Key === true )
        {
            
$key md5$key );
        }

        
$deleteResult $this->_memCache->delete$key );

        if ( 
$this->_doDebug === true )
        {
            
$this->_debug['delete'][$key] = $deleteResult;
        }
        
        return 
$deleteResult;
    }

    
/**
     * Flush all the existing items on the server
     *
     * @access public
     * @return bool
     */

    
public function flush( )
    {
        return 
$this->_memCache->flush( );
    }

    
/**
     * Return the debug information
     *
     * @access public
     * @return array
     */

    
public function returnDebug( )
    {
        if ( 
$this->_doDebug === false )
        {
            return 
false;
        }
        
        
$this->_debug['stats'] = $this->_memCache->getExtendedStats( );
        
$this->_debug['version'] = $this->_memCache->getversion( );
        return 
$this->_debug;
    }

    
/**
     * Close the connection to the memcached server
     *
     * @access public
     * @return bool
     */

    
public function close( )
    {
        if ( !
is_object($this->_memCache) )
        {
            return 
false;
        }

        return 
$this->_memCache->close( );
    }

    
/**
     * Close the connection to the memcached server when the object is destructed
     *
     * @access public
     * @return bool
     */

    
public function __destruct( )
    {
        
$this->close( );
    }
}
?>