扣丁學(xué)堂淺談php緩存技術(shù)及php緩存實現(xiàn)代碼詳解
2017-12-12 14:31:43
1557瀏覽
在現(xiàn)如今,隨著互聯(lián)網(wǎng)時代信息日夜更新,比如有些信息經(jīng)常不變的,但是還是能變的信息放在緩存中以加快顯示速度,這是很有價值的,所謂的緩存,通俗的理解就是一些保存在服務(wù)器端的共用信息.它是于服務(wù)器同生死的,我們在保存緩存的時候可以指定下次更新的時間的判斷,比方要在5分鐘更新一次,那么下面我們一起來看下扣丁學(xué)堂PHP視頻教程之PHP緩存實現(xiàn)代碼的詳細(xì)介紹吧。
數(shù)據(jù)緩存:這里所說的數(shù)據(jù)緩存是指數(shù)據(jù)庫查詢PHP緩存機(jī)制,每次訪問頁面的時候,都會先檢測相應(yīng)的緩存數(shù)據(jù)是否存在,如果不存在,就連接數(shù)據(jù)庫,得到數(shù)據(jù),并把查詢結(jié)果序列化后保存到文件中,以后同樣的查詢結(jié)果就直接從緩存表或文件中獲得。
用的最廣的例子看Discuz的搜索功能,把結(jié)果ID緩存到一個表中,下次搜索相同關(guān)鍵字時先搜索緩存表。
舉個常用的方法,多表關(guān)聯(lián)的時候,把附表中的內(nèi)容生成數(shù)組保存到主表的一個字段中,需要的時候數(shù)組分解一下,這樣的好處是只讀一個表,壞處就是兩個數(shù)據(jù)同步會多不少步驟,數(shù)據(jù)庫永遠(yuǎn)是瓶頸,用硬盤換速度,是這個的關(guān)鍵點(diǎn)。
頁面緩存:每次訪問頁面的時候,都會先檢測相應(yīng)的緩存頁面文件是否存在,如果不存在,就連接數(shù)據(jù)庫,得到數(shù)據(jù),顯示頁面并同時生成緩存頁面文件,這樣下次訪問的時候頁面文件就發(fā)揮作用了。(模板引擎和網(wǎng)上常見的一些PHP緩存機(jī)制類通常有此功能)
時間觸發(fā)緩存:檢查文件是否存在并且時間戳小于設(shè)置的過期時間,如果文件修改的時間戳比當(dāng)前時間戳減去過期時間戳大,那么就用緩存,否則更新緩存。
內(nèi)容觸發(fā)緩存:當(dāng)插入數(shù)據(jù)或更新數(shù)據(jù)時,強(qiáng)制更新PHP緩存機(jī)制。
靜態(tài)緩存:這里所說的靜態(tài)緩存是指靜態(tài)化,直接生成HTML或XML等文本文件,有更新的時候重生成一次,適合于不太變化的頁面,這就不說了。
內(nèi)存緩存:Memcached是高性能的,分布式的內(nèi)存對象PHP緩存機(jī)制系統(tǒng),用于在動態(tài)應(yīng)用中減少數(shù)據(jù)庫負(fù)載,提升訪問速度。
php的緩沖器:有eaccelerator,apc,phpa,xcache,這個這個就不說了吧,搜索一堆一堆的,自己看啦,知道有這玩意就OK
MYSQL緩存:這也算非代碼級的,經(jīng)典的數(shù)據(jù)庫就是用的這種方式,看下面的運(yùn)行時間,0.09xxx之類的
我貼段根據(jù)藍(lán)色那家伙修改后部分my.ini吧,2G的MYISAM表可以在0.05S左右,據(jù)說他前后改了有快一年
基于反向代理的Web緩存:如Nginx,SQUID,mod_proxy(apache2以上又分為mod_proxy和mod_cache)
NGINX的例子
用google找到一些php緩存技術(shù)方法,發(fā)個PHP緩存實現(xiàn),實現(xiàn)了apc和文件緩存,繼承Cache_Abstract即可實現(xiàn)調(diào)用第三方的緩存工具。
isLocked($key)){
return$this;
}
$tries=10;
$count=0;
do{
usleep(200);
$count++;
}while($count<=$tries&&$this->isLocked($key));//最多做十次睡眠等待解鎖,超時則跳過并解鎖
$this->isLocked($key)&&$this->unlock($key);
return$this;
}
}
/**
*APC擴(kuò)展緩存實現(xiàn)
*
*
*@categoryMjie
*@packageCache
*@author流水孟春
*@copyrightCopyright(c)2008-
*@licenseNewBSDLicense
*@version$Id:Cache/Apc.php版本號2010-04-1823:02cmpan$
*/
classCache_ApcextendsCache_Abstract{
protected$_prefix='cache.mjie.net';
publicfunction__construct(){
if(!function_exists('apc_cache_info')){
thrownewCacheException('apcextensiondidn'tinstalled');
}
}
/**
*保存緩存變量
*
*@paramstring$key
*@parammixed$value
*@returnbool
*/
publicfunctionstore($key,$value){
returnapc_store($this->_storageKey($key),$value);
}
/**
*讀取緩存
*
*@paramstring$key
*@returnmixed
*/
publicfunctionfetch($key){
returnapc_fetch($this->_storageKey($key));
}
/**
*清除緩存
*
*@returnCache_Apc
*/
publicfunctionclear(){
apc_clear_cache();
return$this;
}
/**
*刪除緩存單元
*
*@returnCache_Apc
*/
publicfunctiondelete($key){
apc_delete($this->_storageKey($key));
return$this;
}
/**
*緩存單元是否被鎖定
*
*@paramstring$key
*@returnbool
*/
publicfunctionisLocked($key){
if((apc_fetch($this->_storageKey($key).'.lock'))===false){
returnfalse;
}
returntrue;
}
/**
*鎖定緩存單元
*
*@paramstring$key
*@returnCache_Apc
*/
publicfunctionlock($key){
apc_store($this->_storageKey($key).'.lock','',5);
return$this;
}
/**
*緩存單元解鎖
*
*@paramstring$key
*@returnCache_Apc
*/
publicfunctionunlock($key){
apc_delete($this->_storageKey($key).'.lock');
return$this;
}
/**
*完整緩存名
*
*@paramstring$key
*@returnstring
*/
privatefunction_storageKey($key){
return$this->_prefix.'_'.$key;
}
}
/**
*文件緩存實現(xiàn)
*
*
*@categoryMjie
*@packageCache
*@author流水孟春
*@copyrightCopyright(c)2008-
*@licenseNewBSDLicense
*@version$Id:Cache/File.php版本號2010-04-1816:46cmpan$
*/
classCache_FileextendsCache_Abstract{
public$useSubdir=false;
protected$_cachesDir='cache';
publicfunction__construct(){
if(defined('DATA_DIR')){
$this->_setCacheDir(DATA_DIR.'/cache');
}
}
/**
*獲取緩存文件
*
*@paramstring$key
*@returnstring
*/
protectedfunction_getCacheFile($key){
$subdir=$this->useSubdir?substr($key,0,2).'/':'';
return$this->_cachesDir.'/'.$subdir.$key.'.php';
}
/**
*讀取緩存變量
*為防止信息泄露,緩存文件格式為php文件,并以""開頭
*
*@paramstring$key緩存下標(biāo)
*@returnmixed
*/
publicfunctionfetch($key){
$cacheFile=self::_getCacheFile($key);
if(file_exists($cacheFile)&&is_readable($cacheFile)){
//include方式
//returninclude$cacheFile;
//系列化方式
returnunserialize(@file_get_contents($cacheFile,false,NULL,13));
}
returnfalse;
}
/**
*緩存變量
*為防止信息泄露,緩存文件格式為php文件,并以""開頭
*
*@paramstring$key緩存變量下標(biāo)
*@paramstring$value緩存變量的值
*@returnbool
*/
publicfunctionstore($key,$value){
$cacheFile=self::_getCacheFile($key);
$cacheDir=dirname($cacheFile);
if(!is_dir($cacheDir)){
if(!@mkdir($cacheDir,0755,true)){
thrownewCacheException("Couldnotmakecachedirectory");
}
}
//用include方式
//return@file_put_contents($cacheFile,''.serialize($value));
}
/**
*刪除緩存變量
*
*@paramstring$key緩存下標(biāo)
*@returnCache_File
*/
publicfunctiondelete($key){
if(emptyempty($key)){
thrownewCacheException("Missingargument1forCache_File::delete()");
}
$cacheFile=self::_getCacheFile($key);
if(!@unlink($cacheFile)){
thrownewCacheException("Cachefilecouldnotbedeleted");
}
return$this;
}
/**
*緩存單元是否已經(jīng)鎖定
*
*@paramstring$key
*@returnbool
*/
publicfunctionisLocked($key){
$cacheFile=self::_getCacheFile($key);
clearstatcache();
returnfile_exists($cacheFile.'.lock');
}
/**
*鎖定
*
*@paramstring$key
*@returnCache_File
*/
publicfunctionlock($key){
$cacheFile=self::_getCacheFile($key);
$cacheDir=dirname($cacheFile);
if(!is_dir($cacheDir)){
if(!@mkdir($cacheDir,0755,true)){
if(!is_dir($cacheDir)){
thrownewCacheException("Couldnotmakecachedirectory");
}
}
}
//設(shè)定緩存鎖文件的訪問和修改時間
@touch($cacheFile.'.lock');
return$this;
}
/**
*解鎖
*
*@paramstring$key
*@returnCache_File
*/
publicfunctionunlock($key){
$cacheFile=self::_getCacheFile($key);
@unlink($cacheFile.'.lock');
return
以上就是關(guān)于php緩存技術(shù)及php緩存實現(xiàn)代碼的詳細(xì)介紹,最后想要學(xué)習(xí)PHP開發(fā)技術(shù)的小伙伴不要猶豫了,扣丁學(xué)堂是
PHP培訓(xùn)技術(shù)的最佳選擇,想要PHP視頻教程的小伙伴現(xiàn)在就聯(lián)系我們的咨詢老師領(lǐng)取吧,機(jī)會總是留給有準(zhǔn)備的人的!扣丁學(xué)堂
PHP開關(guān)工程師技術(shù)交流群:374332265。
【關(guān)注微信公眾號免費(fèi)領(lǐng)取丁豆獲取更多的學(xué)習(xí)資料】
查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>
標(biāo)簽:
PHP開發(fā)工程師
PHP培訓(xùn)
PHP視頻教程
PHP在線視頻
php緩存技術(shù)