Рефакторинг смартРекрейтер API

1

В настоящее время я реализую smartrecruiter api в своем проекте. Я использую две конечные точки, а именно /jobs-list и /job-details. Проблема в том, что каждый раз, когда я извлекаю детали во второй конечной точке, которая является /job-details, время выполнения так медленно.

Изображение 174551

Вот что я сделал до сих пор:

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;
}
Теги:

1 ответ

0
Лучший ответ

Решил его, кэшируя функцию для извлечения данных:

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;
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню