diff --git a/index.php b/index.php index bd59951..dd124de 100644 --- a/index.php +++ b/index.php @@ -1,17 +1,27 @@ 0,'url' => $url,'name' => $name]; - echo json_encode($arr); - } else { - header('Location:'.$url); - } +$memcache = new Memcache; +$memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); +$json = $memcache->get('randUrlJson'); +// echo "获取缓存"; +if(trim($json) == "" || $json == false){ + if(file_exists($file_path)){ + $fp = fopen($file_path,"r"); + $str = fread($fp,filesize($file_path)); + $memcache->set('randUrlJson', $str); + $json = $str; + // echo "设置缓存"; + } +} +$res = json_decode($json,true); +$randNum = rand(0,sizeof($res)-1); +$url = $res[$randNum]['url']; +$name = $res[$randNum]['name']; +if($_GET['return'] == 'json'){ + $arr = ['code' => 0,'url' => $url,'name' => $name]; + echo json_encode($arr); +} else { + // echo $url; + header('Location:'.$url); } \ No newline at end of file