getCached($url, $expires = 3600);

I find myself working with a lot of Kirby projects that require calls to external APIs or feeds. This plugin is a simple helper function that adds a caching layer to f::read().

getCached($url, $expires = 3600);

  • $url (string)
    The path for the request
  • $expires (int)
    Number of seconds until the cache expires (optional; defaults to 1 hour)

Use getCached() as a drop-in replacement for Kirby’s f::read() or PHP’s file_get_contents().

2 Likes

Did you know that you can use Kirby’s built-in cache for your custom data as well?

$cache = kirby()->cache()->get('your.cache.key');
if($cache) return $cache;

// get the value
// ...

kirby()->cache()->set('your.cache.key', $value, $minutes);
return $value;

Of course the cache needs to be enabled for this to work.

Good to know.

This plugin actually works very well in cases where you don’t want to have the general cache enabled.

For example, advanced search queries where parameters are used to get a certain subset of feed data. In that case the same page/URL might be serving up different content on each page load – but getCached() can cache individual request results.