Controlling the cache?

Hello,

Is there a way to have control of the cache of kirby?

Several of my pages will have their contents updated using an external API. I will want to make an API call every hour (at the 55th minute of every hour), then cache the page (including the API results) and serve the cached version until the next update.

Is this possible using the kirby cache? If yes, how?

You might be able to utilize what Kirby does itself after updating a page to refresh the cache:

cache::flush();
$page->reset();
$page->touch();

So maybe you can do something like that at the end of your API call / refresh.

EDIT: if you use page->update() the cache should be refreshed anyways.

2 Likes

Hello,

Thanks for your answer.

I now got to the point where I am implementing the cache part and I am having a bit of trouble with fine control.

If I do page(‘some-page’)->update(); (for any page) the whole cache is flushed, so the result is the same as with cache::flush();

I would prefer a way to remove the cache for specific pages only.

I tried the cache::remove() function, but that takes the whole URL (in MD5) including http and the query string in the end. This means that if that page is accessed in a few different ways, e.g. http://www.example.com/some-page/ and http://www.example.com/some-page/?x=3, if I do

cache::remove(md5('http://www.example.com/some-page/'));

then only that specific URL will be removed from the cache, and not all the other ways of accessing “some-page”.

Is there any way to target the cache based on the unique id (some-page) instead of the whole url?

I’m not aware of one. Personally, I guess flushing the whole cache wouldn’t be too bad.

I want to flush the cache every night with a cronjob. I have an event calendar and a RSS feed on my homepage with the latest entries.
Flushing the entire cache isn’t a problem for me.

Is there a method in Kirby to flush the entire cache?

http://getkirby.com/docs/toolkit/api/cache-driver/flush

1 Like

Thanks @texnixe. What is the preferred way to hook this into a cronjob?

I guess make an api template like described here? http://getkirby.com/blog/json

1 Like

You can simply create a cron.php next to your index.php with the following content:

<?php

define('DS', DIRECTORY_SEPARATOR);

// load kirby
require(__DIR__ . DS . 'kirby' . DS . 'bootstrap.php');

$kirby = kirby();
$kirby->configure();
$kirby->cache()->flush();

This can be called in the cronjob with curl for example: http://yourdomain.com/cron.php
You might want to add some kind of token check though, so only your cronjob can really flush your cache.

7 Likes

Thanks for your explanation Bastian!