How to cache custom data programmatically?

@dmitry_fedyuk is there anyway I can cache data in Magento 2?

Let’s example: I am reading XML file youtube video and display it on site, I want to cache it, how to do in magento 2?

public function getVideo()
{
$xmlContent=file_get_contents(“https://www.youtube.com/feeds/videos.xml?channel_i=test”);
$xmlElement = simplexml_load_string($xmlContent,‘SimpleXMLElement’);
$urls =[];
$i=0;
foreach($xmlElement->entry as $entry)
{
$url = $entry->link[‘href’];
$url = explode(’?v=’,$url);
if($i<=20){
$urls[]=$url[1];
}
$i++;
}

  return $urls;

}

Use \Magento\Framework\App\CacheInterface.

1 Like