php檔案快取資料_PHP教程

來源:互聯網
上載者:User
在做網吧看看的時候,由於頁面中存在電影的搜尋功能(使用者輸入)

這個功能由於不能夠做成靜態化,那麼就只能夠動態,用動態時候會對資料庫,伺服器壓力帶來很大的考驗

所以就只能用到快取資料的方式了

資料緩衝的形式包括:

1、將資料緩衝到記憶體,相信大家這個就會想到了Memcached.memcached是高效能的分布式記憶體快取服務器。 一般的使用目的是,通過快取資料庫查詢結果,減少資料庫訪問次數,以提高動態Web應用的速度、 提高可擴充性。

2、用檔案來快取資料.將資料儲存到檔案中,用key=>value的形式來儲存,key指檔案名稱.這個地方必須要保證key的唯一性

設定檔案的緩衝時間,如果過時了就從資料庫中得到資料並儲存到檔案中,

下面是一個檔案快取類:

1、快取資料

2、得到資料

3、判斷快取資料是否存在

4、刪除某個快取資料

5、清除過時的快取資料

6、清除所以的快取資料

class Inc_FileCache{

private $cacheTime = 3600; //預設緩衝時間

private $cacheDir = CACHE_DIR; //緩衝絕對路徑

private $md5 = true; //是否對鍵進行加密

private $suffix = ".php"; //設定檔案尾碼

public function __construct($config){

if( is_array( $config ) ){

foreach( $config as $key=>$val ){

$this->$key = $val;

}

}

}

//設定緩衝

public function set($key,$val,$leftTime=null){

$key = $this->md5 ? md5($key) : $key;

$leftTime = $leftTime ? $leftTime : $this->cacheTime;

!file_exists($this->cacheDir) && mkdir($this->cacheDir,0777);

$file = $this->cacheDir.'/'.$key.$this->suffix;

$val = serialize($val);

@file_put_contents($file,$val) or $this->error(__line__,"檔案寫入失敗");

@chmod($file,0777) or $this->error(__line__,"設定檔案許可權失敗");

@touch($file,time()+$leftTime) or $this->error(__line__,"變更檔時間失敗");

}

//得到緩衝

public function get($key){

$this->clear();

if( $this->_isset($key) ){

$key_md5 = $this->md5 ? md5($key) : $key;

$file = $this->cacheDir.'/'.$key_md5.$this->suffix;

$val = file_get_contents($file);

return unserialize($val);

}

return null;

}

//判斷問件是否有效

public function _isset($key){

$key = $this->md5 ? md5($key) : $key;

$file = $this->cacheDir.'/'.$key.$this->suffix;

if( file_exists($file) ){

if( @filemtime($file) >= time() ){

return true;

}else{

@unlink($file);

return false;

}

}

return false;

}

//刪除檔案

public function _unset($key){

if( $this->_isset($key) ){

$key_md5 = $this->md5 ? md5($key) : $key;

$file = $this->cacheDir.'/'.$key_md5.$this->suffix;

return @unlink($file);

}

return false;

}

//清除到期快取檔案

public function clear(){

$files = scandir($this->cacheDir);

foreach ($files as $val){

if (@filemtime($this->cacheDir."/".$val) < time()){

@unlink($this->cacheDir."/".$val);

}

}

}

//清除所有快取檔案

public function clear_all(){

$files = scandir($this->cacheDir);

foreach ($files as $val){

@unlink($this->cacheDir."/".$val);

}

}

private function error($line,$msg){

die("出錯檔案:".__file__."/n出錯行:$line/n錯誤資訊:$msg");

}

}

在頁面中的調用方法如下:

$cacheFile = new Inc_FileCache(array('cacheTime'=>60,'suffix'=>'.php'));

//得到電影熱播榜

$where = " where pid=75";

$moviehotModel = $this->getM('moviehot');

$moviehotCount = $moviehotModel->getCount($where);

if( !$cacheFile->_isset($where.$moviehotCount.'moviehot') ){

$moviehotResult = $moviehotModel->getList(" WHERE pid=75 ",'0,10',"orderby desc");

if(count($moviehotResult) > 0) {

$cacheFile->set($where.$moviehotCount.'moviehot',$moviehotResult);

}

}else{

$moviehotResult = $cacheFile->get($where.$moviehotCount.'moviehot');

}

$this->tpl['moviehotResult'] = $moviehotResult;

大家如果還有什麼好的檔案快取的代碼可以拿來共用一下

摘自 ms_X0828的專欄

http://www.bkjia.com/PHPjc/478509.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478509.htmlTechArticle在做網吧看看的時候,由於頁面中存在電影的搜尋功能(使用者輸入) 這個功能由於不能夠做成靜態化,那麼就只能夠動態,用動態時候會對資料...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.