PHP 開發 APP 介面 學習筆記與總結 - APP 介面執行個體 [3] 首頁 APP 介面開發方案 ② 讀取緩衝方式

來源:互聯網
上載者:User

標籤:

以靜態緩衝為例。

 

修改 file.php line:11 去掉 path 參數(方便),加上緩衝時間參數:

public function cacheData($k,$v = ‘‘,$cacheTime = 0){     //檔案名稱      $filename = $this->_dir.$k.‘.‘.self::EXT;      .....

line:25 把緩衝時間設定為 11 位的數字,如果不滿 11 位,則在時間前面補 0。再把緩衝時間和緩衝內容進行拼接:

$cacheTime = sprintf(‘%011d‘,$cacheTime);    //$cacheTime 設定為11位(方便截取),不滿11位前面補0//把緩衝時間拼接$vreturn file_put_contents($filename,$cacheTime.json_encode($v));

測試一下:

$file = new Cache();$file->cacheData(‘data‘,‘index-data‘,50);

開啟 data.txt:

00000000050"index-data"

 

再修改讀取緩衝 line33:

$contents = file_get_contents($filename);$cacheTime = (int)substr($contents,0,11);$val = substr($contents,11);if($cacheTime != 0 && $cacheTime+filemtime($filename) < time()){    //緩衝已經失效    unlink($filename);    return false;}return json_decode($val,true);

 

在 list.php 中引入 file.php

修改 line:14

$cache = new Cache();$vals = array();if(!$vals = $cache->cacheData(‘index-data‘.$page.‘-‘.$pageSize)){    //echo ‘aaaa‘;exit(); //測試緩衝失效    try{        $connect = DB::getInstance()->connect();    }catch(Exception $e){        return Response::show(403,‘資料庫連接失敗‘);    }    $res = mysql_query($sql,$connect);        while($val = mysql_fetch_assoc($res)){        $vals[] = $val; //二維數組    }    if($vals){        $cache->cacheData(‘index-data‘.$page.‘-‘.$pageSize,$vals,50);    }

測試頁面:

http://127.0.0.17/php/APP/list.php?pageSize=10&page=3

緩衝失效時(沒有注釋echo ‘aaaa‘;exit(); ),頁面輸出:aaa

 

list.php

<?phprequire_once ‘response.php‘;require_once ‘db.php‘;require_once ‘file.php‘;$page = isset($_GET[‘page‘])?$_GET[‘page‘]:1;$pageSize = isset($_GET[‘pageSize‘])?$_GET[‘pageSize‘]:1;if(!is_numeric($page) || !is_numeric($pageSize)){    return @Response::show(401,‘資料不合法‘);}$offset = ($page-1)*$pageSize; //每頁起始數$sql = ‘select * from review where is_enabled = 1 order by creation_time desc limit ‘.$offset.‘,‘.$pageSize;$cache = new Cache();$vals = array();//當沒有緩衝或者緩衝失效時,串連資料庫並從資料庫中取出資料//注意當有分頁的資料時,需要把分頁資訊寫入檔案名稱if(!$vals = $cache->cacheData(‘index-data‘.$page.‘-‘.$pageSize)){    //echo ‘aaaa‘;exit(); //測試緩衝失效    try{        $connect = DB::getInstance()->connect();    }catch(Exception $e){        return Response::show(403,‘資料庫連接失敗‘);    }    $res = mysql_query($sql,$connect);        while($val = mysql_fetch_assoc($res)){        $vals[] = $val; //二維數組    }    //同時把取出的資料存入緩衝    if($vals){        $cache->cacheData(‘index-data‘.$page.‘-‘.$pageSize,$vals,50);    }}//如果緩衝存在同時沒有失效,使用封裝的介面類封裝緩衝中的資料if($vals){    return Response::show(200,‘首頁資料擷取成功‘,$vals);}else{    return Response::show(400,‘首頁資料擷取失敗‘,$vals);}

測試 http://127.0.0.17/php/APP/list.php?pageSize=10&page=3 產生 index-data3-10.txt

測試 http://127.0.0.17/php/APP/list.php?pageSize=10 產生 index-data1-10.txt

 

附:file.php:

 1 <?php 2 class Cache{ 3     //靜態快取檔案尾碼名 4     const EXT = ‘txt‘; 5     //定義快取檔案存放路徑 6     private $_dir; 7     public function __construct(){ 8         $this->_dir = dirname(__FILE__).‘/files/‘; 9     }10 11     public function cacheData($k,$v = ‘‘,$cacheTime = 0){ //預設永久不失效12         //檔案名稱13         $filename = $this->_dir.$k.‘.‘.self::EXT;14         //$v不為‘’:儲存緩衝或者刪除緩衝15         if($v !== ‘‘){16             //刪除緩衝17             if(is_null($v)){18                 return @unlink($filename);19             }20             //儲存緩衝21             $dir = dirname($filename);22             if(!is_dir($dir)){23                 mkdir($dir,0777);24             }25             $cacheTime = sprintf(‘%011d‘,$cacheTime);    //$cacheTime 設定為11位(方便截取),不滿11位前面補026             //把緩衝時間拼接$v27             return file_put_contents($filename,$cacheTime.json_encode($v));28         }29         //讀取緩衝30         if(!is_file($filename)){31             return false;32         }33         $contents = file_get_contents($filename);34         $cacheTime = (int)substr($contents,0,11);35         $val = substr($contents,11);36         if($cacheTime != 0 && $cacheTime+filemtime($filename) < time()){    //緩衝已經失效37             unlink($filename);38             return false;39         }40         return json_decode($val,true);41     }42 }
View Code

 

 

 

 

 

參考:

PHP中的sprintf()函數的學習研究筆記

 

PHP 開發 APP 介面 學習筆記與總結 - APP 介面執行個體 [3] 首頁 APP 介面開發方案 ② 讀取緩衝方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.