Cache part of a page?

i just create a file in site/cache myself and read data (json in my case) back if needed. so i do not need to enable cache for complete site.

$data = null;
$cacheFile = kirby()->roots()->cache().DS.md5('cache-for-something').'.json';
if(!f::exists($cacheFile) ||
  ( f::exists($cacheFile) && (time() - f::modified($cacheFile) > c::get('cache.refresh', 360)) )  // seconds
) {
    $data = []; // get data somehow
    f::write($cacheFile, json_decode($data));
} else {
    $data = json_decode(f::read($cacheFile));
}
3 Likes