/*
* 姓名: wrapperCache
* 備註: fileCache、memcache/memcached、APC、Xcache 和eacheelsator 的包裝緩存, array (array('host'=>'localhost')));
echo $cacheObj->cache('key','value');
*/
classwrapperCache {
3
const CACHE_TYPE_AUTO = '自動';
const CACHE_TYPE_EACCELERATOR = 'eaccelerator'; = ' apc';
const CACHE_TYPE_MEMCACHE = 'memcache';
const CACHE_TYPE_MECHEC = 'filecache';
const CACHE_TYPE_XCACHE = 'xcache';
$cache_expire; //快取過期的秒數 private $cache_type;/ / 總是只回傳一個實例
public static function getInstance($type=self:: CACHE_TYPE_AUTO, $exp_time=3600, $params='cache/'){
//快取建構函數,選用過期時間與快取路徑
private function __construct($type, $exp_time, $ params) {
公共職能
}
// 阻止使用者複製實例
public function __clone(){
$this->cacheError ('不允許複製。 ');
}
//從資料夾中刪除快取 public functionclearCache(){_pide ) case self::CACHE _TYPE_EACCELERATOR:
self::CACHE_TYPE_XCACHE :
xcache_clear_cache(XC_TYPE_VAR , 0);
中斷;
$this->cache_external->flush();
中斷;
$ this->cache_external->flush();
中斷;
$this->cache_external->deleteCache(); break;if ($value != '') { //想寫
if ($ttl == '') $ttl = $this->cache_expire; ttl);
} else return $this->get($key);
//讀取值
}
// 快取一個使用給新設定的快取檔案,的名稱,要儲存的資訊/值的資料 private function put($key, $data, $ttl = '') {
if ($ttl == '') $ttl = $this-> ;cache__m ;
eaccelerator_put($key, serialize($data), $ ttl);
休息;
case self::CACHE_TYPE_APC : apc_store($key, $data, $ttl);
case self::CACHE_TYPE_XCACHE :
xcache_set($key, 序列化($data), $ttl );
中斷;
c data=serialize($data);
$this->cache_external->set($key, $data, false , $ttl) ;
中斷;
case S ize($data);
$this->cache_external->set($key, $data, $ttl);
中斷;
case self::CACHE_TYPE_FILE :
$this->cache_external->cache($key,$data);
休息;
}
}
//返回給定鍵的快取
private function get($key){
switch($this->cache_type){
$data = 反序列化(eaccelerator_get ($key));
中斷;
case self::CACHE_TYPE_APC : case self::CACHE_TYPE_APC : ($key);
中斷;
unserialize(xcache_get($ key)); 中斷; = unserialize( $this->cache_external->get($key));
個案自我:: CACHE_TYPE_FILE :
$data = $this-> cache_external->cache($key);
中斷;
}
//從快取中刪除金鑰
公用函數刪除($key){
switch($this->cache_type){
case self::CACHE_TYPE_EACCELERATOR :
);
中斷;
case key);
中斷;
c _unset($key);
中斷;
case self::CACHE_TYPE_MEMCACHE :
中斷;
case self::CACHE_TYPE_MEMCACHED : ($key);
中斷;
// 重載應用程式變數並自動快取
public function __set($name, $value) {
$this->put($name, $value, $this->cache_expire );
公用函數__get($name) {
return $this->get($name);
}
case self::CACHE_TYPE_APC :
中斷;
case self:: CACHE_TYPE_XCACHE : if (function_exists('xcache_get')) $this->cacheError('未找到Xcache');;案例self::CACHE_TYPE_MEMCACHE :
if (Meclass_ex if' )) $this->init_mem();
else $this->cacheError('memcache 找不到');
個案自我:: CACHE_TYPE_MEMCACHED :
案例self::CACHE_TYPE_FILE :
if (class_exists('fileCache '))$this->init_filecache();
else $this->cacheError('fileCache 找不到');
break;
case unction_exists('eaccelerator_get')) $this->cache _type = self::CACHE_TYPE_EACCELERATOR;
. 'apc_fetch')) $this->cache_type = self::CACHE_TYPE_APC ;
elseif (function_exists(') _paches('xc_paches)( CHE;
elseif (class_exists('Memcache ')) $ this->init_mem();
elseif (class_exists('Memcached')) class_exists('file)快取')) $this->init_filecache ();
else $this->cacheError('找不到任何相容的快取');
break;
$msg='未選擇任何快取類型' ;
if (isset($type)) $msg='選擇了無法辨識的快取類型'.$type.'';
break; } }
) 創造詞
. ached類別的實例if (is_array($this-> ;cache_params)) { if ($useMemecached) {
$this->cache_type = self::CACHE_TYPE_MEMCACHED;
} else {
$this ->cache_type = self::CACHE_TYPE_MEMCACHE;
$this->cache_external = new Memcache;
}
_ ache para ache$server['port'] = isset($server['連接埠']) ? (int)$server['port'] : self::DEFAULT_MEMCACHE_PORT;
if ($useMemecached) { $server['host'], $server['port '] );
} else {
, $.H) = 持久')')[]]] $this->cache_external-> addServer($server['host'], $server['port'], $server['persistent']) ;
}
約 ache ache 三酸 } $this->cache_type . '需要一個陣列,例如:wrapperCache::getInstance("' . $this->cache_type . '", 30,array(array("host"=>"localhost")));');
}
private function init_filecache(){//取得filecache 類別的實例 $this-> ;cache_type = self::CACHE_TYPE_FIache;paches; this->cache_expire, $this ->cache_params); }public function getAvailableCache($ return_format='html'){// 返回可用快取
); $avCaches[] = array(self: :CACHE_TYPE_EACCELERATOR,function_exists('eaccelerator_get'));$avCaches[] = array(self::CACHE_TYPE_APC, function_exists('apc_fetch')) ;
CHE ists('xcache_get'));
$avCaches[] = array(self::CACHE_TYPE_MEMCACHE, class_exists('Memcache'));
$avCaches[] = array(self::CA? [] = array(self::CACHE_TYPE_FILE , class_exists('fileCache'));
if ($return_format == ' foreach ($avCaches為$c) {
$ ret .= '
if ($c[1]) ret不相容';
$ret .= ' ';
} else return $avCaches;
}
原生樹函數cacheError($msg){//觸發錯誤 trigger_error('