修改代码
public function cacheData($key,$value='',$cacheTime=0){
$filename=$this->_dir.$key.self::EXT;
$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+filemtime($filename)<time()){
unlink($filename);
return FALSE;
}
return json_decode($value,true);
require_once 'file.php';
$cache=new File();
$tests = array();
if(!$tests=$cache->cacheData('index_mk_cache'.$page.'-'.$pagesize)) {
if($tests){
$cache->cacheData('index_mk_cache'.$page.'-'.$pagesize,$tests,120);
}
}
file.php
<?php
class File{
private $_dir;
const EXT='.txt';
public function __construct()
{
$this->_dir=dirname(__FILE__).'/files/';
}
public function cacheData($key,$value='',$cacheTime=0){
$filename=$this->_dir.$key.self::EXT;
if($value!==''){//将value值写入缓存
if($cacheTime!=0&&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+filemtime($filename)<time()){
unlink($filename);
return FALSE;
}
return json_decode($value,true);
}
}
$file=new File();
echo $file->cacheData('test1');
list.php
<?php
//http://app.com/list.php?page=1&pagesize=12
require_once 'response.php';
require_once 'Db.php';
require_once 'file.php';
$page=isset($_GET['page'])?$_GET['page']:1;
$pagesize=isset($_GET['pagesize'])?$_GET['pagesize']:15;
if(!is_numeric($page)||!is_numeric($pagesize)){
return Response::show(401,"数据不合法");
}
$offset=($page-1)*$pagesize;
$sql="select * from test order by id desc limit ".$offset.",".$pagesize;
$cache=new File();
$tests = array();
if(!$tests=$cache->cacheData('index_mk_cache'.$page.'-'.$pagesize)) {
try {
$connect = Db::getInstance()->connect();
} catch (Exception $e) {
//$e->getMessage 建议只用于调试 不暴露信息给客户端
return Response::show(403, "数据库连接失败");
}
$result = mysql_query($sql, $connect);
while ($item = mysql_fetch_assoc($result)) {
$tests[] = $item;
}
if($tests){
$cache->cacheData('index_mk_cache'.$page.'-'.$pagesize,$tests,120);
}
}
if ($tests) {
return Response::show(200, '首页数据获取成功', $tests);
} else {
return Response::show(400, '首页获取数据失败', $tests);
}
?>
Db.php
<?php
class Db{
static private $_instance;
static private $_connectSource;
private $_dbConfig=array(
'host'=>'127.0.0.1',
'user'=>'root',
'password'=>'',
'database'=>'info'
);
private function __construct()
{
}
static public function getInstance(){
if(!(self::$_instance instanceof self)){
self::$_instance=new Db();
}
return self::$_instance;
}
public function connect(){
if(!self::$_connectSource){
self::$_connectSource=@mysql_connect($this->_dbConfig['host'],$this->_dbConfig['user'],$this->_dbConfig['password']);
//echo self::$_connectSource;
if(!(self::$_connectSource)){
throw new Exception('mysql connect error');
//die('mysql connect error'.mysql_error());
}else{
mysql_select_db($this->_dbConfig['database'],self::$_connectSource);
mysql_query('set name UTF8');}
}
return self::$_connectSource;
}
}
// $connect=Db::getInstance()->connect();
//
// $sql="select * from test";
// echo mysql_num_rows(mysql_query($sql,$connect));
?>
response.php
<?php
class Response
{
const JSON="json";
public static function show($code,$message='',$data=array(),$type=self::JSON){
if(!is_numeric($code)){
return '';
}
$type=isset($_GET['format'])?$_GET['format']:self::JSON;
$result=array(
'code'=>$code,
'message'=>$message,
'$data'=>$data
);
if($type=='json'){
self::json($code,$message,$data);
ecit;
}else if($type=='array'){
var_dump($result);
}else if ($type=='xml'){
self::xmlEncode($code,$message,$data);
}else{
//TODO
}
}
public static function json($code, $message = '', $data = array())
{
if(!is_numeric($code)){
return "";
}else{
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
$str=json_encode($result);
echo preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $str);
exit;
}
}
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="<?xml version='1.0' encoding='UTF-8'?>";
$xml.="<root>";
$xml.=self::xmlToEncode($result);
$xml.="</root>";
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 .= "</{$key}>";
}
return $xml;
}
}
?>