首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >重构智能招聘程序api

重构智能招聘程序api
EN

Stack Overflow用户
提问于 2016-01-29 05:03:48
回答 1查看 311关注 0票数 1

我目前正在我的项目中实现智能招聘api。我使用两个端点,即/工作-名单/职务-详情。问题是,每次我在第二个端点(即作业-细节)中提取细节时,执行时间都很慢。

以下是我迄今所做的工作:

代码语言:javascript
运行
复制
function getContext() 
{
    $opts = array(
      'http'=> array(
            'method' => 'GET',
            'header' => 'X-SmartToken: xxxxxxxxxxxxxxxxx'     
        )
    );
    return $context = stream_context_create($opts); 
}

function getSmartRecruitmentJob($city, $department) 
{
    $tmp    = array();
    $results= array();
    $limit  = 100; //max limit for smartrecruiter api is 100

    // Open the file using the HTTP headers set above
    $file = file_get_contents('https://api.smartrecruiters.com/jobs?limit='.$limit.'&city='.$city.'&department='.$department, false, $this->getContext());
    $lists= json_decode($file, true);

    foreach($lists['content'] as $key => $list) 
    {
        if ($list['status'] == 'SOURCING' || $list['status'] == 'INTERVIEW' || $list['status'] == 'OFFER') 
        {
            $results['id'] = $list['id'];
            $tmp[] = $this->getSmartRecruitmentJobDetails($results['id']);  
        }
    }
    return $tmp;
}


function getSmartRecruitmentJobDetails($id) 
{
    $results  = array();
    $file = file_get_contents('https://api.smartrecruiters.com/jobs/'.$id, false, $this->getContext());
    $lists= json_decode($file, true);

    $results['title']            = isset($lists['title']) ? $lists['title'] : null;
    $results['department_label'] = isset($lists['department']['label']) ? $lists['department']['label'] : null;
    $results['country_code']     = isset($lists['location']['countryCode']) ? $lists['location']['countryCode'] : null;
    $results['city']             = isset($lists['location']['city']) ? $lists['location']['city'] : null;
    $results['url']              = isset($lists['actions']['applyOnWeb']['url']) ? $lists['actions']['applyOnWeb']['url'] : null;
    return $results;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-01 03:11:40

通过缓存提取数据的函数来解决这个问题:

代码语言:javascript
运行
复制
function getCache() 
{
    if ($this->cache === null) 
    {
        $cache = \Zend\Cache\StorageFactory::factory(
         array(
          'adapter' => array(
           'name' => 'filesystem',
           'options' => array(
            'ttl' => 3600 * 7, // 7 hours
            'namespace' => 'some-namespace',
            'cache_dir' => 'your/cache/directory'
           ),
          ),
          'plugins' => array(
           'clear_expired_by_factor' => array('clearing_factor' => 10),
          ),
         )
        );  

        $this->cache = $cache;
    }
    return $this->cache;
}

function getSmartRecruitmentJobDetails($id) 
{

    $cache = $this->getCache();
    $key = md5('https://api.smartrecruiters.com/jobs/'.$id);
    $lists = unserialize($cache->getItem($key, $success));
    $results  = array();

    if($success && $lists) 
    {
        header('Debug-cache-recruit: true');
    }
    else 
    {
        header('Debug-cache-recruit: false');
        // Open the file using the HTTP headers set above
        $file = file_get_contents('https://api.smartrecruiters.com/jobs/'.$id, false, $this->getContext());
        $lists= json_decode($file, true);
        $cache->addItem($key, serialize($lists));   
    }

    $results['title']            = isset($lists['title']) ? $lists['title'] : null;
    $results['department_label'] = isset($lists['department']['label']) ? $lists['department']['label'] : null;
    $results['country']      = isset($lists['location']['country']) ? $lists['location']['country'] : null;
    $results['country_code']     = isset($lists['location']['countryCode']) ? $lists['location']['countryCode'] : null;
    $results['city']             = isset($lists['location']['city']) ? $lists['location']['city'] : null;
    $results['url']              = isset($lists['actions']['applyOnWeb']['url']) ? $lists['actions']['applyOnWeb']['url'] : null;
    return $results;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35077253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档