Commit f87415f6 authored by Isaque Melo's avatar Isaque Melo
Browse files

adiciona cache

parent 9240c4fa
......@@ -10,16 +10,19 @@ if($instance['youtube_list'] == 'playlist'){
$url = "https://www.googleapis.com/youtube/v3/search?key={$api_key}&channelId={$list_id}&maxResults={$num}&part=snippet,id&order=date";
}
$cache_key = __FILE__ . ':' . $url;
if(apcu_exists($cache_key)){
$result = apcu_fetch($cache_key);
} else {
$cache_key = "featured-videos:" . md5($url);
if(!($result = wp_cache_get($cache_key))){
$result = json_decode(file_get_contents($url));
apcu_store($cache_key, $result, 300);
if(is_object($result) && !$result->error){
wp_cache_add($cache_key, $result, '', 30 * MINUTE_IN_SECONDS);
} else {
$result = (object) ['items' => []];
wp_cache_add($cache_key, $result, '', 3 * MINUTE_IN_SECONDS);
}
}
if (!is_object($result) || count($result->items) === 0) {
$result = (object) ['items' => []];
if(!$result->items){
return;
}
$extract_vid = function($video){
......@@ -29,6 +32,7 @@ $extract_vid = function($video){
return $video->id->videoId;
}
};
?>
<div class="custom-so-widget alignfull featured-videos">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment