App interface development and read static cache, app read static cache
1
Php2 /**3 * Description:app Interface4 * Create date:2015-10-19 13:365 * Author:zhaoyingnan6 **/7 classResponse8 {9 /**Ten * Integrated Approach One * @author Zhaoyingnan 2015-10-19 11:24 A * @param int $iCode status Code - * @param string $SMSG tip information - * @param mix $mixData Data the * @param string $sType interface return type - * @return String - **/ - Static functionShow$iCode,$SMSG= '',$mixData= '',$sType= ' json ') + { - if(!Is_numeric($iCode)) + return''; A $arData=Array( at' Code ' =$iCode, -' Message ' =$SMSG, -' Data ' =$mixData - ); - Switch($sType) - { in Case' Array ': - Echo''; to Print_r ($arData); + Echo '
'; - Break; the Case' XML ': *Self::xml ($arData); $ Break;Panax Notoginseng default: -Self::json ($arData); the } + } A the /** + * JSON - * @author Zhaoyingnan 2015-10-19 10:21 $ * @param array $arData $ * @return String - **/ - Private functionJson$arData=Array()) the { - Exit(Json_encode ($arData));Wuyi } the - /** Wu * XML - * @author Zhaoyingnan 2015-10-19 10:21 About * @param array $arData $ * @return String - **/ - Private functionxml$arData=Array()) - { A Header(' Content-type:text/xml '); + $sXml= ''; the $sXml.= "
\ n "; - $sXml.= "
\ n "
;
$
$sXml . = Self::xmlencode (
$arData
);
the
$sXml . = "
\ n "; the Exit($sXml); the } the - /** in * XML encode the * @author Zhaoyingnan 2015-10-19 11:10 the * @param array $arData About * @return String the **/ the Private functionXmlencode ($arData=Array()) the { + if(!$arData) - return''; the $sXml=$sAttr= '';Bayi foreach($arData as $mKey=$mVal) the { the if(Is_numeric($mKey)) - { - $sAttr= "Id= ' {$mKey}'"; the $mKey= ' Item '; the } the $sXml.=Is_array($mVal) ? Self::xmlencode ($mVal): "<{$mKey}{$sAttr}>{$mVal}
$mKey}> "; the } - return $sXml; the } the } the?>
1
Php2 /**3 * Description: Static cache4 * Create date:2015-10-19 13:365 * Author:zhaoyingnan6 **/7 class file8 {9 Private $sExt= '. txt ';Ten One /** A * Generate/delete/get Cache - * @author Zhaoyingnan 2015-10-19 11:33 - * @param string $sKey file name the * @param mix $mixValue Cached data (for "when the cache is taken, NULL when the cache file is deleted, otherwise the cache is generated) - * @param string $sPath The path saved by the file - * @param int $iCacheTime cache time (seconds), 0 is never expired - * @return Boolean + **/ - Public functionCacheData ($sKey,$mixValue= '',$sPath= '/alidata/www/lianxi/file/',$iCacheTime= 0) + { A $sPath=RTrim($sPath, '/').' /'; at $sFileName=$sPath.$sKey.$this-sExt; - //Generate cache Files - if($mixValue) - { - if(!Is_dir($sPath)) - mkdir($sPath, 0777); in $iCacheTime=sprintf('%011d ',$iCacheTime); - return file_put_contents($sFileName,$iCacheTime. Json_encode ($mixValue)); to } + - if(Is_file($sFileName) &&!$mixValue) the { * if(Is_null($mixValue)) $ { Panax Notoginseng //Delete Cache - return unlink($sFileName); the } + A //Get Cache the $sContent=file_get_contents($sFileName); + $iTime=intval(substr($sContent, 0, 11)); - $sContent=substr($sContent, 11); $ if($iTime! = 0 &&$iTime+Filemtime($sFileName) < Time()) $ { - //Expired, deleted - unlink($sFileName); the return FALSE; - } Wuyi return $sContent; the } - Else Wu { - return FALSE; About } $ } - } -?>
1
Php2 include' Response.php ';3 include' File.php ';4 $_get[' format '] =isset($_get[' Format ']) &&In_array($_get[' Format '],Array(' xml ', ' json ', ' array ') ?$_get[' Format ']: ' JSON ';5 $file=New File();6 //Delete Cache7 //exit (Var_dump ($file->cachedata (' Index_cache ', null) ));8 9 if(!$sContent=$file->cachedata (' Index_cache '))Ten { One //echo "Get cache failed \ n"; A //echo "Get data \ n"; - $arData=Array( -' id ' = 1, the' Name ' = ' Teddynan ', -' Sex ' = 23, - Array( -' Nani ' =Array( +' g ' = ' GG ', -2, +4 A ) at ) - ); - //echo "Generate Cache \ n"; - - $file->cachedata (' Index_cache ',$arData, '/alidata/www/lianxi/file/', 0); -Response::show (0, ' success ',$arData,$_get[' Format ']); in } - Else to { +Response::show (0, ' success ', Json_decode ($sContent,TRUE),$_get[' Format ']); - } the?>
http://www.bkjia.com/PHPjc/1061852.html www.bkjia.com true http://www.bkjia.com/PHPjc/1061852.html techarticle app interface development and read static cache, app read static cache 1? PHP 2/* * 3 * Description:app Interface 4 * Create date:2015-10-19 13:36 5 * author:zhaoying Nan 6 * */7 class Res ...