日期:2014-05-17  浏览次数:20443 次

强大的PHP缓存类(eaccelerator、apc、xcache、memcache模块缓存)
<?php
/*
 * Name:    wrapperCache
 * URL:     http://www.admpub.com/
 * Version: v0.1
 * Date:    29/10/2010
 * Author:  Chema Garrido
 * License: GPL v3
 * Notes:   wrapper cache for fileCache, memcache, APC, Xcache and eaccelerator
 */
 
class wrapperCache {
    private $cache_params;//extra params for external caches like path or connection option memcached
    public $cache_expire;//seconds that the cache expires
    private $cache_type;//type of cache to use
    private $cache_external; //external instance of cache, can be fileCache or memcache
    private static $instance;//Instance of this class
         
    // Always returns only one instance
    public static function GetInstance($type='auto',$exp_time=3600,$params='cache/'){
        if (!isset(self::$instance)){//doesn't exists the isntance
             self::$instance = new self($type,$exp_time,$params);//goes to the constructor
        }
        return self::$instance;
    }
     
    //cache constructor, optional expiring time and cache path
    private function __construct($type,$exp_time,$params){
        $this->cache_expire=$exp_time;
        $this->cache_params=$params;
        $this->setCacheType($type);
    }
     
    public function __destruct() {
        unset($this->cache_external);
    }
     
    // Prevent users to clone the instance
    public function __clone(){
        $this->cacheError('Clone is not allowed.');
    }
     
    //deletes cache from folder
    public function clearCache(){
        switch($this->cache_type){
            case 'eaccelerator':
                @eaccelerator_clean();
                @eaccelerator_clear();
            break;
 
            case 'apc':
                apc_clear_cache('user');
            break;
 
            case 'xcache':
                xcache_clear_cache(XC_TYPE_VAR, 0);
            break;
 
            case 'memcache':
                @$this->cache_external->flush();
            break;
             
            case 'filecache':
                $this->cache_external->deleteCache();
            break;
        }  
    }
     
    //writes or reads the cache
    public function cache($key, $value='',$ttl=''){
        if ($value!=''){//wants to write
            if ($ttl=='') $ttl=$this->cache_expire;
            $this->put($key, $value,$ttl);
        }
        else return $this->get($key);//reading value
    }
     
    //creates new cache files with the given data, $key== name of the cache, data the info/values to store
    private function put($key,$data,$ttl='' ){
        if ($ttl=='') $ttl=$this->cache_expire;
         
        switch($this->cache_type){
            case 'eaccelerator':
                eaccelerator_put($key, serialize($data), $ttl);
            break;
 
            case 'apc':
                apc_store($key, $data, $ttl);
            break;
 
            case 'xcache':
                xcache_set($key, serialize($data), $ttl);
            break;
 
            case 'memcache':
                $data=serialize($data);
                if (!$this->cache_external->replace($key, $data, false, $ttl))
                $this->cache_external->set($key, $data, false, $ttl);
            break;
             
            case 'filecache':
                $this->cache_external->cache($key,$data);
            break;
        }  
    }
     
    //returns cache for the given key
    private function get($key){
        switch($this->cache_type){
            case 'eaccelerator':
                $data =  @unserialize(eaccelerator_get($key));
            break;
 
            case 'apc':
                $data =  apc_fetch($key);
            break;
 
            case 'xcache':
                $data =  @unserialize(xcache_get($key));
            break;
 
            case 'memcache':
                $data = @unserialize($this->cache_external->get($key));