流程: 從數(shù)據(jù)庫(kù)獲取信息 -> 封裝 -> 生成接口數(shù)據(jù) 應(yīng)用場(chǎng)景:數(shù)據(jù)時(shí)效性,比較高的系統(tǒng) 學(xué)習(xí)要點(diǎn): 1. 掌握如何獲取數(shù)據(jù); 2. 掌握如何將獲取的數(shù)據(jù)生成通信數(shù)據(jù); 詳細(xì)流程: http請(qǐng)求 -> 服務(wù)器 -> 查詢(xún)數(shù)據(jù) -> 返回?cái)?shù)據(jù)
文件鏈接:var/www/app/list.php
// 封裝輸出數(shù)據(jù)類(lèi)require_once('./response.php');// 引入的 db.php 數(shù)據(jù)庫(kù)類(lèi)需要進(jìn)行改動(dòng)require_once('./db.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, '數(shù)據(jù)不合法');}$offset = ($page-1) * $pageSize;$sql = "select * from mk_user where score != 0 order by id desc limit " . $offset . "," . $pageSize;// $sql = "select * from mk_user";// $result = MySQL_query($sql, $connect);// echo mysql_fetch_row($result);// var_dump($result);try { $connect = Db::getInstance()->connect();} catch(Exception $e) { return Response::show(403, '數(shù)據(jù)庫(kù)連接失敗', $users);}$result = mysql_query($sql, $connect);$users = array();while ($user = mysql_fetch_assoc($result)) { $users[] = $user;}if($users) { return Response::show(200, '首頁(yè)數(shù)據(jù)獲取成功', $users);} else { return Response::show(400, '首頁(yè)數(shù)據(jù)獲取失敗', $users);}文件鏈接:var/www/app/db.php ~~~ class Db { static PRivate
} ~~~
流程:從數(shù)據(jù)庫(kù)獲取信息 -> 封裝(讀取緩存,再次讀取) -> 返回?cái)?shù)據(jù) 用途:減少數(shù)據(jù)庫(kù)壓力 學(xué)習(xí)要點(diǎn): 1. 掌握靜態(tài)緩存如何設(shè)置緩存失效時(shí)間; 2. 如何設(shè)置緩存; 詳細(xì)流程: 
文件連接:var/www/app/list.php ~~~
流程:http請(qǐng)求 -> 緩存(crontab從數(shù)據(jù)庫(kù)獲取緩存) -> 封裝并返回?cái)?shù)據(jù) 學(xué)習(xí)要點(diǎn): 1. 掌握如何編寫(xiě)定時(shí)腳本程序 2. 理解服務(wù)器如何提前準(zhǔn)備數(shù)據(jù) 
增加crontab執(zhí)行文件:var/www/app/cron.php
// 讓crontab定時(shí)執(zhí)行的腳本程序 */5 * * * * /usr/bin/php /var/www/app/cron.php// 獲取user表中6條數(shù)據(jù)require_once('./db.php');require_once('./file.php');$sql = "select * from mk_user where score != 0 order by id desc";try { $connect = Db::getInstance()->connect();} catch(Exception $e) { // $e->getMessage(); // 鏈接失敗,在日志文件里記錄失敗的記錄 file_put_contents('./logs/' . date('y-m-d') . '.txt', $e->getMessage()); return;}$result = mysql_query($sql, $connect);$users = array();while ($user = mysql_fetch_assoc($result)) { $users[] = $user;}$file = new File();if($users) { // 有數(shù)據(jù)的話,把數(shù)據(jù)寫(xiě)到緩存文件里 $file->cacheData('index_cron_cache', $users);} else { file_put_contents('./logs/' . date('y-m-d') . '.txt', "沒(méi)有相關(guān)數(shù)據(jù)");}return;上面的crontab沒(méi)5分鐘執(zhí)行一次,當(dāng)有數(shù)據(jù)時(shí),在list.php文件開(kāi)頭能夠查詢(xún)到數(shù)據(jù):var/www/app/list.php
require_once('./response.php');require_once('./file.php');// 增加下面對(duì)緩存進(jìn)行查詢(xún)$file = new File();$data = $file->cacheData('index_cron_cache');if ($data) { return Response::show(200, '首頁(yè)數(shù)據(jù)獲取成功', $data);} else { return Response::show(400, '首頁(yè)數(shù)據(jù)獲取失敗', $data);}require_once('./db.php');新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注