Disable cache if cached file is from yesterday

On this site. https://aic.cologne, we have quite a big sitetree as all initiatives can manage their own events in closed-off subtrees of the site (site->initiatives->initiative->exhibitions->events).

on the homepage, all events are merged into one big calendar which is then rendered. to make this work fast we enabled the cache. however, there is this ‘TODAY’ flag on some events, and since the cached file might be from yesterday, the calendar on the homepage is sometimes wrong. we are looking for a solution to re-render the page/ignore the cache if the cached file is from yesterday but are struggling a little bit on how to do this. in pseudocode this would work using something like this (in config.php):

return [
    //...other settings
    'cache' => [
        'pages' => [
            'active' => true,
            'ignore' => function ($page) {
                return isHomePage() && cachedFileIsFromYesterday()
            }
        ]
    ]
];

However we could not find a proper way of determining if “cachedFileIsFromYesterday()”. How do i find the cached file? How do i find out if it is from yesterday. any help would be highly appreciated!

many greetings from cologne,
Tilman

Here’s an annotated snippet from my archives that may be of use:

// the page ID and content type to check from cache
$uri = 'path/to/a-page'; // or $page->id() in your case
$contentType = 'html';

// replicated from https://github.com/getkirby/kirby/blob/ddd11f9b4a23e0b5d306b146ca5d6b7a1c1909a9/src/Cms/Page.php#L267
$cacheId = [page($uri)->id()];
if (kirby()->multilang() === true) {
  $cacheId[] = kirby()->language()->code();
}
$cacheId[] = $contentType;
$cacheId = implode('.', $cacheId);

// returns either the page cache's epoch timestamp or `false`
$cacheTimestamp = kirby()->cache('pages')->modified($cacheId);

But the pictured idea of ignoring the cache if cachedFileIsFromYesterday() returns true would mean your calendar page will never get cached again after day one. Maybe consider a (pseudo) cronjob or other solution? As a minimum, your function would have to delete the expired cache. You can selectively delete that single page using kirby()->cache('pages')->remove($cacheId), once you have determined the Cache ID as pictured above.

Not tested, but it might even be possible to extend the FileCache class in a plugin to set a specific lifespan for this particular page’s cache or something like that.

thanks, that’s very close to what we came up with!