Cache or content representation for more efficient, continual page queries

I’m building a site for a community area which will act like a community board, showing what local events are coming up next today and into the future. The homepage will take that list of events and show titles, images, dates, etc so visitors can see what’s going on today (but not what’s already happened today). Both the home page and the calendar pages will show these upcoming events, refreshed every half-hour or so.

Also, each event could have several dates/times as in a local theatre which shows a play several times a day over the span of a few weeks. All of these event dates are stored in a structure field inside an event.

If we have 100+ events, and I want to know what’s next every half hour or so, would it be more efficient to query for the list of events and times using a content representation, or caching this list in a file cache that we update?

I don’t see how a content representation would be useful here. But it would certainly make sense to cache the list. Considering that you are using the same list in two locations, it might however make sense to store the list in a custom cache and refresh every 30 minutes, and query this cache in both of your templates.

1 Like

consider using my lapse plugin. it can automatically take care of updating the cache if the cached kirby objects are modified.

Thank you @bnomei! That looks like a great plugin. In this case though, I think I’ll have to break the cache every 30min because I need to show what’s coming next in the daily calendar, as time passes. So if it’s 3pm when a user visits the site, I only want to show events happening at 3pm that day or later into the future. Then at 4pm I only want to show events happening at 4 or later.

A cron job that clears the cache every 30 mins. would probably be a good idea.

1 Like

the laspe plugin can devalidate a cache every 30 minutes just fine. just provide a custom key as well as the objects to be tracked.

$key30Minutes = date('YmdH') . ( (date('i') > 30 ) ? '30' : '00' ) ;
$data = Lapse::io(
    [$key30Minutes, $page,  $collection], // will create key from array of objects
    function () use ($page, $collection) {
        // ... generate data based on current time... 3pm, 4pm whatever

@bnomei I’d like that approach as I wouldn’t have to mess with server settings on local, staging, live, etc.

sorry i do not understand. so did lapse and my $key30Minutes help or not?

@bnomei I’m sorry I haven’t been able to try it yet, but as soon as I can I will let you know :slightly_smiling_face:

1 Like