為什麼要使用緩存技術?理由很簡單:提高效率。在程序開發中,獲取信息的方式主要是查詢數據庫,除此以外,也可能是通過Web Services或者別的某種方法,無論哪種方法,在大量的並發訪問面前,它們都可能成為效率的瓶頸,為了解決這些問題,人們提出了很多解決方案,其中一些是利用優化軟件(如:APC,Eaccelerator,Zend Optimizer等等)來提高程序的運行效率,合理的運用這些軟件,往往能使程序的運行效率得到數量級上的提升,但前提是你必須擁主機的控制權,以便能夠安裝這些軟件,如果你使用的是虛擬主機的話,那麼只能祈禱你的服務提供商已經預裝了某個優化軟件,否則就必須自己使用PHP來實現相應的緩存功能。如果這讓你感到無所適從,相信下面的這段緩存操作類的代碼能給你一些有用的啟發。(PHP緩存操作實例詳解)
<?php
/**
+----------------------------------------------------------
* franklin 緩存操作類
+----------------------------------------------------------
* 文件名稱 cache.php
+----------------------------------------------------------
* 文件描述 緩存操作類
+----------------------------------------------------------
* 作 者 http://www.phpernote.com
+----------------------------------------------------------
* 時 間 2012-05-05
+----------------------------------------------------------
*/
class cache{
//查詢參數
protected $options=array();
//緩存次數
protected $cacheCount=0;
//緩存時間
protected $cachetime=60;
//緩存路徑
protected $cachePath='cache/';
//數據返回類型, 1代表數組, 2代表對象
protected $returnType=1;
/**
* 讀取緩存
* @param string $id 緩存名稱
* @param int $time 緩存有效時間,默認為60秒,0為永遠失效
* @param string $path緩存文件存放路徑
* @accesspublic readCache
* @returnmixed如果讀取成功返回緩存內容, 否則返回NULL
*/
public function readCache($id,$time,$Path=''){
$id=md5($id);
$this->cachePath=emptyempty($Path)?$this->cachePath:$Path;
$this->cachetime=emptyempty($time)?$this->cachetime:$time;
$file=$this->cachePath.$id.'.php';
if(file_exists($file)){
//緩存過期
if((filemtime($file)+$time)<time()){
@unlink($file);
return NULL;
}
if(1===$this->returnType){
$row=include $file;
}else{
$data=file_get_contents($file);
$row=unserialize($data);
}
return $row;
}
return NULL;
}
/**
* 寫入緩存
*
* @accesspublic
* @param mixed$data緩存內容
* @returnbool是否寫入成功
*/
public function writeCache($id,$data,$Path=''){
$this->cacheCount++;
$id=md5($id);
$this->cachePath=emptyempty($Path)?$this->cachePath:$Path;
$file=$this->cachePath.$id.'.php';
chmod($this->cachePath,777);
if(1===$this->returnType){
$data='<?php return '.var_export($data, TRUE).'; ?>';
}else{
$data=serialize($data);
}
return file_put_contents($file, $data);
}
/**
* 刪除指定緩存
*
* @accesspublic
* @param mixed$id緩存名稱
* @returnbool是否刪除成功
*/
public function delCache($id,$Path=''){
$id=md5($id);
$this->cachePath=emptyempty($Path)?$this->cachePath:$Path;
$file=$this->cachePath.$id.'.php';
if(file_exists($file)){
return unlink($file);
}
return NULL;
}
/**
* 刪除所有緩存
*
* @accesspublic
* @param mixed$dir緩存名稱
* @returnbool清除所有緩存是否成功
*/
public function delAllCache($Path=''){
$id=md5($id);
$this->cachePath=emptyempty($Path)?$this->cachePath:$Path;
$path=$this->cachePath;
if(is_dir($path)){
if($dh=opendir($path)){
while(($file=readdir($dh))!==false){
if($file!='..'&$file!='.'){
if(is_dir($path.'/'.$file)){
if(!delDir($path.'/'.$file)){
return 0;
}
}else{
if(!unlink($path.'/'.$file)){
return 0;
}
}
}
}
closedir($dh);
}
return 1;
}
}
}
以上緩存操作類的引用方法如下:
<?php
include('cache.php');
$data=array('http://www.phpernote.com','http://www.baidu.com','http://www.google.cn');
$cache=new cache();
$id='test';
//寫入緩存
$cache->writeCache($id,$data);
//讀緩存並打印
$name_row=$cache->readCache($id,120);
print_r($name_row);
//刪除某個變量
$cache->delCache($id);
//刪除全部緩存
$cache->delAllCache();
注意要保證cache目錄(即緩存目錄)存在並且可寫。