Yar请求数据接口

    //[['u'=>'site.index','d'=>['a'=>2],'k'=>'test']];
    public function apiBatch($arr,$timeout=15000) {
        ini_set("yar.timeout",$timeout);
        $url = C('RPCURL');
        foreach ($arr as  $v){
            $k = $v['k'] ? $v['k']:$v['u'];
            $cacheKey = md5(http_build_query($v));
            if(!$v['t'] || !is_array($this->$k = S($cacheKey))){
                \Yar_Concurrent_Client::call($url."?key=".$k."|".$cacheKey, 'api', $v, [$this, "callback"]);
            }
        }
        \Yar_Concurrent_Client::loop([$this, "callback"], [$this, "error_callback"]);
    }
    
    //['u'=>'site.index','d'=>[],'t'=>200] 缓存时间秒
    public function api($arr,$timeout=15000) {
        ini_set("yar.timeout",$timeout);
        $url = C('RPCURL');
        $cacheKey = md5(http_build_query($arr));
        $cacheTime = isset($arr['t']) ? $arr['t'] : 300;
        if(!$cacheTime || (!$result = S($cacheKey))){
            $client = new \Yar_Client($url);
            $result = $client->api($arr['u'],$arr['d']);
            if($cacheTime && $result['data']){
                S($cacheKey,$result,  $cacheTime);
            }
        }
        return $result;
    }

 

posted @ 2018-06-27 17:25  路闻man  阅读(257)  评论(0编辑  收藏  举报