C5-8.3: How to cache an XML file from remote server?

Permalink
To prevent frequent remote server requests for an XML file which is updated infrequently, how can I cache the XML file from that server, so that it's downloaded only once a day for example?

How do I make sure it's reloaded on cache expiry? Does cache handle it by itself? Does it make it expired or I have to keep track of it?

Thank you.

linuxoid
 
mesuva replied on at Permalink Reply
mesuva
I'd recommend reviewing the documentation on caching:
https://documentation.concrete5.org/developers/caching/overview...

This functionality handles what you've described, you can put _something_ into the cache with an expiry and it stores it for you. You'd pretty much follow the example in the doc, but instead of doing something like the $list = StudentList::findBy(array('course_id' => $id)); line, you'd be doing your remote call to fetch your XML and storing that instead.
linuxoid replied on at Permalink Reply
linuxoid
Yep, I think I go it:
public function getXml() 
    {
        $expensiveCache = $this->app->make('cache/expensive');
        $cacheItem = $expensiveCache->getItem('MYPACKAGE');
        if ($cacheItem->isMiss()) {
            @$XML = file_get_contents("http://www.SITE.com.FILE.xml");
            if($XML == FALSE) {
                return '';
            }
            $expensiveCache->save($cacheItem->set($XML)->expiresAfter(300));
            $this->status = $this->status_latest;
        }
        else {
            $XML = $cacheItem->get();
            $this->status = $this->status_cached;

Thanks.