国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

PHP開(kāi)發(fā)APP接口(五):首頁(yè)APP接口開(kāi)發(fā)

2019-11-06 08:19:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

方案一:讀取數(shù)據(jù)庫(kù)方式開(kāi)發(fā)首頁(yè)接口

流程: 從數(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 instance;//保存類(lèi)的實(shí)例staticprivate_connectSource; // 連接的資源 private $_dbConfig = array( ‘host’ => ‘192.168.2.110’, ‘user’ => ‘root’, ‘passWord’ => ‘root’, ‘database’ => ‘muke’, );

// 構(gòu)造函數(shù)需要設(shè)置成私有,防止被其他類(lèi)實(shí)例化private function __construct() {}// 訪問(wèn)實(shí)例的公共方法static public function getInstance() { // 是否實(shí)例,如果沒(méi)有則實(shí)例化類(lèi) if (!self::$_instance instanceof self) { self::$_instance = new self(); } return self::$_instance;}public function connect() { if(!self::$_connectSource) { self::$_connectSource = mysql_connect($this->_dbConfig['host'], $this->_dbConfig['user'], $this->_dbConfig['password']); if(!self::$_connectSource) { // 此處注意,如果數(shù)據(jù)庫(kù)連接失敗,需要拋出一個(gè)異常,以供連接時(shí)使用,連接需要用 try-catch 測(cè)試連接。 throw new Exception('mysql connect error ' . mysql_error()); // die('mysql connect error'. mysql_error()); } mysql_select_db($this->_dbConfig['database'], self::$_connectSource); mysql_query("set names UTF8", self::$_connectSource); return self::$_connectSource; }}

} ~~~


方案二:讀取緩存方式開(kāi)發(fā)首頁(yè)接口

流程:從數(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 ~~~

方案三:定時(shí)讀取緩存方式開(kāi)發(fā)首頁(yè)接口

流程: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');
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 康定县| 塔河县| 黄龙县| 尖扎县| 西青区| 河北区| 兰溪市| 安西县| 卫辉市| 泰州市| 神木县| 汾西县| 沧州市| 渑池县| 牟定县| 会宁县| 肃南| 织金县| 永兴县| 连平县| 临邑县| 阜阳市| 扎兰屯市| 介休市| 马边| 上犹县| 礼泉县| 赤壁市| 龙岩市| 措勤县| 北辰区| 沁水县| 深圳市| 南投县| 泰兴市| 镇安县| 鹿邑县| 循化| 天全县| 卢氏县| 尉犁县|