public static function getAll(array $args = []) { $max = '+inf'; $min = '-inf'; $limit = 20; if (!empty($args['limit'])) { $limit = intval($args['limit']); } $offset = 0; if (!empty($args['offset'])) { $offset = intval($args['offset']); } $flag = ''; if (!empty($args['flag'])) { $flag = $args['flag']; } $dma_code = null; $dma_cache_key = ''; // request DMA jobs cache if user requested jobs by location if (!empty($args['code']) && ($args['code'] >= 500 && $args['code'] <= 881)) { $dma_code = $args['code']; $dma_cache_key = self::getJobsByDmaCahceKey($dma_code); self::setJobsByDmaCache($dma_code); } else { if (!empty($args['location'])) { // lookup dma code $dma_code = DmaMap::getDmaCode($args['location']); if (!empty($dma_code)) { $args['code'] = $dma_code; // will be return as a part of a payload self::setJobsByDmaCache($dma_code); $dma_cache_key = self::getJobsByDmaCahceKey($dma_code); } } } $kw_list = []; $kw_cache_list = []; $cache_key_union_out = ''; $result_type = ''; $cache_key_intersect_out = 'intersect'; $cache_key_union_out = 'union'; $union_out = ''; $key_out = ''; $intersect_out = ''; $intersect_in = []; // request KEYWORD jobs caches if user requested jobs by keyword if (!empty($args['keyword'])) { $req_k = explode(' ', strtolower($args['keyword'])); foreach ($req_k as $k) { $kw = trim($k); if (!empty($kw) && !in_array($kw, $kw_list)) { $cache_key_union_out .= ':' . $kw; $cache_key_intersect_out .= ':' . $kw; $kw_list[] = $kw; } } if (!empty($kw_list)) { foreach ($kw_list as $ck) { // request jobs by keyword $kw_cache_key = Keyword::getKeywordJobsCacheKey($ck); Keyword::setKeywordJobsCache($ck); $kw_cache_list[] = $kw_cache_key; // request jobs by company keyword $kw_company_cache_key = Keyword::getKeywordCompaniesCacheKey($ck); Keyword::setKeywordCompaniesCache($ck); $all_companies_str_ids = Redis::smembers($kw_company_cache_key); if (!empty($all_companies_str_ids)) { foreach ($all_companies_str_ids as $c_str_id) { $company_jobs_cache = Company::getCompanyJobsCacheKey($c_str_id); Company::setCompanyJobsCache($c_str_id); $kw_cache_list[] = $company_jobs_cache; } } } } } if (!empty($dma_code)) { // DMA - Yes if (!empty($kw_cache_list)) { // Keyword - Yes if (count($kw_cache_list) == 1) { // only one keyword - intersect vars $intersect_out = $cache_key_intersect_out . ':' . $dma_code; $intersect_in = [$kw_cache_list[0], $dma_cache_key]; $key_out = $intersect_out; } else { // multiple keywords foreach ($kw_cache_list as $kin) { $keys_in[] = $kin; } // union first $union_out = $cache_key_union_out; Redis::zunionstore($union_out, $keys_in, ['aggregate' => 'sum']); Redis::expire($union_out, 60 * 5); // intersect vars $intersect_out = $cache_key_intersect_out . ':' . $dma_code; $intersect_in = [$union_out, $dma_cache_key]; $key_out = $intersect_out; } Redis::zinterstore($intersect_out, $intersect_in, ['aggregate' => 'sum']); Redis::expire($intersect_out, 60 * 5); $interstore_total = Redis::zcard($key_out); if ($interstore_total > 0) { // we got result $key_out = $intersect_out; $result_type = 'kw&dma'; } else { $dma_total = Redis::zcard($dma_cache_key); if ($dma_total > 0) { $key_out = $dma_cache_key; $result_type = 'dma'; } else { $key_out = self::getRecentJobsCacheKey(); self::setRecentJobsCache(); $result_type = 'recent'; } } } else { // Keyword - No $dma_total = Redis::zcard($dma_cache_key); if ($dma_total > 0) { $key_out = $dma_cache_key; $result_type = 'dma'; } else { $key_out = self::getRecentJobsCacheKey(); self::setRecentJobsCache(); $result_type = 'recent'; } } } else { // DMA No if (!empty($kw_cache_list)) { // Keyword - Yes if (count($kw_cache_list) == 1) { // only one keyword return keyword cache $key_out = $kw_cache_list[0]; } else { // multiple keywords foreach ($kw_cache_list as $kin) { $keys_in[] = $kin; } // union first $union_out = $cache_key_union_out; Redis::zunionstore($union_out, $keys_in, ['aggregate' => 'sum']); Redis::expire($union_out, 60 * 5); $key_out = $union_out; } $union_total = Redis::zcard($key_out); if ($union_total > 0) { // we got result $result_type = 'kw'; } else { $key_out = self::getRecentJobsCacheKey(); self::setRecentJobsCache(); $result_type = 'recent'; } } else { // Keyword - No $key_out = self::getRecentJobsCacheKey(); self::setRecentJobsCache(); $result_type = 'recent'; } } $final_list = []; $total = Redis::zcard($key_out); if ($flag == 'random') { if ($total > $limit) { $offset = rand(0, $total - $limit); } } $keys = Redis::zrevrangebyscore($key_out, $max, $min, ['limit' => [$offset, $limit]]); if (!empty($keys)) { foreach ($keys as $job_id) { $job_info = self::getInfo($job_id, self::$defaultFields); if (!empty($job_info['JobID'])) { $final_list[] = $job_info; } } } return ['total' => $total, 'jobs' => $final_list, 'result_type' => $result_type, 'args' => $args]; }