簡易的設置緩存

一個簡單的緩存設置方式

/**
 * 簡易的設置緩存
 * @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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章