一:读取数据库方式开发首页接口原理图 从数据库获取信息--->>>>封装------>>>生成接口数据 备注:运用场景[数据时性比较高的系统]二:代码实现(1)list.php cacheData('index_cron_cahce');if($data) { return Response::show(200, '首页数据获取成功', $data);}else{ return Response::show(400, '首页数据获取失败', $data);}exit;require_once('./db.php');require_once('./file.php');$page = isset($_GET['page']) ? $_GET['page'] : 1;$pageSize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 6;if(!is_numeric($page) || !is_numeric($pageSize)) { return Response::show(401, '数据不合法');}$offset = ($page - 1) * $pageSize;$sql = "select * from video where status = 1 order by orderby desc limit ". $offset ." , ".$pageSize;$cache = new File();$videos = array();if(!$videos = $cache->cacheData('index_mk_cache' . $page .'-' . $pageSize)) { echo 1;exit; try { $connect = Db::getInstance()->connect(); } catch(Exception $e) { // $e->getMessage(); return Response::show(403, '数据库链接失败'); } $result = mysql_query($sql, $connect); while($video = mysql_fetch_assoc($result)) { $videos[] = $video; } if($videos) { $cache->cacheData('index_mk_cache' . $page .'-' . $pageSize, $videos, 1200); }}if($videos) { return Response::show(200, '首页数据获取成功', $videos);} else { return Response::show(400, '首页数据获取失败', $videos);}-------------------------response.php------------- $code, 'message' => $message, 'data' => $data, ); if($type == 'json') { self::json($code, $message, $data); exit; } elseif($type == 'array') { var_dump($result); } elseif($type == 'xml') { self::xmlEncode($code, $message, $data); exit; } else { // TODO } } /** * 按json方式输出通信数据 * @param integer $code 状态码 * @param string $message 提示信息 * @param array $data 数据 * return string */ public static function json($code, $message = '', $data = array()) { if(!is_numeric($code)) { return ''; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data ); echo json_encode($result); exit; } /** * 按xml方式输出通信数据 * @param integer $code 状态码 * @param string $message 提示信息 * @param array $data 数据 * return string */ public static function xmlEncode($code, $message, $data = array()) { if(!is_numeric($code)) { return ''; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data, ); header("Content-Type:text/xml"); $xml = " \n"; $xml .= "\n"; $xml .= self::xmlToEncode($result); $xml .= " "; echo $xml; } public static function xmlToEncode($data) { $xml = $attr = ""; foreach($data as $key => $value) { if(is_numeric($key)) { $attr = " id='{ $key}'"; $key = "item"; } $xml .= "<{ $key}{ $attr}>"; $xml .= is_array($value) ? self::xmlToEncode($value) : $value; $xml .= " \n"; } return $xml; }}-----------------file.php------------------- _dir = dirname(__FILE__) . '/files/'; } public function cacheData($key, $value = '', $cacheTime = 0) { $filename = $this->_dir . $key . self::EXT; if($value !== '') { // 将value值写入缓存 if(is_null($value)) { return @unlink($filename); } $dir = dirname($filename); if(!is_dir($dir)) { mkdir($dir, 0777); } $cacheTime = sprintf('%011d', $cacheTime); return file_put_contents($filename,$cacheTime . json_encode($value)); } if(!is_file($filename)) { return FALSE; } $contents = file_get_contents($filename); $cacheTime = (int)substr($contents, 0 ,11); $value = substr($contents, 11); if($cacheTime !=0 && ($cacheTime + filemtime($filename) < time())) { unlink($filename); return FALSE; } return json_decode($value, true); }}$file = new File();echo $file->cacheData('test1');