APP接口开发及读取静态缓存

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1<?php2/**3* Description: App 接口4* Create date:2015-10-19 13:365* Author: zhaoyingnan6**/7classResponse8{9/**10* 综合方式11* @author zhaoyingnan 2015-10-19 11:2412* @param int $iCode 状态码13* @param string $sMsg 提示信息14* @param mix $mixData data15* @param string $sType 接口返回类型16* @return string17**/18staticfunctionshow($iCode,$sMsg= '',$mixData= '',$sType= 'json')19{20if(!is_numeric($iCode))21return'';22$arData=array(23'code' =>$iCode,24'message' =>$sMsg,25'data' =>$mixData26);27switch($sType)28{29case'array':30echo'<PRe>';31print_r($arData);32echo'</pre>';33

break;34case'xml':35self::xml($arData);36

break;37default:38self::json($arData);39}40}4142/**43* json44* @author zhaoyingnan 2015-10-19 10:2145* @param array $arData46* @return string47**/48privatefunctionjson($arData=array())49{50exit(json_encode($arData));51}5253/**54* xml55* @author zhaoyingnan 2015-10-19 10:2156* @param array $arData57* @return string58**/59privatefunctionxml($arData=array())60{61header('Content-Type:text/xml');62$sXml= '';63$sXml.= "<?xml version='1.0' encoding='UTF-8'?>\n";64$sXml.= "<root>\n";65$sXml.= self::xmlEncode($arData);66$sXml.= "</root>\n";67exit($sXml);68}6970/**71* xml encode72* @author zhaoyingnan 2015-10-19 11:1073* @param array $arData74* @return string75**/76privatefunctionxmlEncode($arData=array())77{78if(!$arData)79return'';80$sXml=$sAttr= '';81foreach($arDataas$mKey=>$mVal)82{83if(is_numeric($mKey))84{85$sAttr= " id='{$mKey}'";86$mKey= 'item';87}88$sXml.=is_array($mVal) ? self::xmlEncode($mVal) : "<{$mKey}{$sAttr}>{$mVal}</{$mKey}>";89}90return$sXml;91}92}93?>

1<?php2/**3* Description: 静态缓存4* Create date:2015-10-19 13:365* Author: zhaoyingnan6**/7classfile8{9private$sExt= '.txt';1011/**12* 生成/删除/获取 缓存13* @author zhaoyingnan 2015-10-19 11:3314* @param string $sKey 文件名15* @param mix $mixValue 被缓存的数据(为''时表示获取缓存,为NUll时为删除缓存文件,否则为生成缓存)16* @param string $sPath 文件保存的路径17* @param int $iCacheTime 缓存时间(秒),0为永不过期18* @return boolean19**/20publicfunctioncacheData($sKey,$mixValue= '',$sPath= '/alidata/www/lianxi/file/',$iCacheTime= 0)21{22$sPath=rtrim($sPath, '/').'/';23$sFileName=$sPath.$sKey.$this->sExt;24//生成缓存文件25if($mixValue)26{27if(!is_dir($sPath))28mkdir($sPath, 0777);29$iCacheTime=sprintf('%011d',$iCacheTime);30returnfile_put_contents($sFileName,$iCacheTime.json_encode($mixValue));31}3233if(is_file($sFileName) && !$mixValue)34{35if(is_null($mixValue))36{37//删除缓存38returnunlink($sFileName);39}4041//获取缓存42$sContent=file_get_contents($sFileName);43$iTime=intval(substr($sContent, 0, 11));44$sContent=substr($sContent, 11);45if($iTime!= 0 &&$iTime+filemtime($sFileName) <time())46{47//过期了,删除48unlink($sFileName);49returnFALSE;50}51return$sContent;52}53else54{55returnFALSE;56}57}58}59?>

1<?php2include'response.php';3include'file.php';4$_GET['format'] =isset($_GET['format']) &&in_array($_GET['format'],array('xml', 'json', 'array')) ?$_GET['format'] : 'json';5$file=newFile();6//删除缓存7//exit(var_dump($file->cacheData('index_cache', null)));89if(!$sContent=$file->cacheData('index_cache'))10{11//echo "获取缓存失败\n";12//echo "获取数据\n";13$arData=array(14'id' => 1,15'name' => 'TeddyNan',16'sex' => 23,17array(18'nani'=>array(19'g'=>'gg',202,21422)23)24);25//echo "生成缓存\n";2627$file->cacheData('index_cache',$arData, '/alidata/www/lianxi/file/', 0);28Response::show(0, 'success',$arData,$_GET['format']);29}30else31{32Response::show(0, 'success', json_decode($sContent,TRUE),$_GET['format']);33}34?>

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航