一个简单的缓存设置方式
/**
* 简易的设置缓存
* @param 缓存目录 $sPath
* @param http请求的url $sUrl
* @param 缓存时间 $sTime
* @param 是否开启缓存 $openCache
* @return array
*/
function setCache($sPath,$sUrl,$sTime,$openCache='1',$sDateType='array')
{
$nowtime = time();
if (file_exists ( $sPath ) && ($nowtime - filemtime($sPath) < $sTime) && $openCache) {
$aData = include ( $sPath ) ;
} else {
if($sDateType == 'json'){
$aData = json_decode ( curl_httpget ( $sUrl ) ,true);
}else{
//默认array
$aData = unserialize ( curl_httpget ( $sUrl ) );
}
if(!empty($aData))
{
$sDate = '<?php' . "\n" . 'return ' . var_export( $aData, true ) . ';' . "\n" . '?>';
file_put_contents ( $sPath, $sDate );
}else{
if(file_exists($sPath))
{
$aData = include ( $sPath ) ;
}
//error_log('请求数据为空:'.$sUrl."\r\n", 3, INDEX_CACHE_PATH.'urlerr.log');
}
}
return $aData;
}
转载于:https://blog.51cto.com/cbsizhang/1588301