Rest API Cache in Kirby v3

Hey,

Can you point me in the right direction? ā€” I am not sure if the docs give me enough informationā€¦
How would I cache a kirby rest api call on my own site?
Is something easy like

ā€˜cacheā€™ => [
ā€˜apiā€™ => [
ā€˜activeā€™ => true
]
]

available?
How would I then clean the cache after a page is updated? ā€” I was thinking about hooking into page.update:after with cache->remove($key), but what would be the $key for an API call?

Thx!

Hi,

From the docs:

1: Set up a custom cache ā€œapiā€ in your config.php like this:

/site/config.php
return [
  'cache.api' => true
];

2: Use your cache

// e.g. in a controller
return function ($kirby) {

  $apiCache = $kirby->cache('api');
  $apiData  = $apiCache->get('apiData');

  // there's nothing in the cache, so let's fetch it
  if ($apiData === null) {
    $apiData = Remote::get('https://someapi.com');
    $apiCache->set('apiData', $apiData);
  }

  return compact('apiData');

};

Now thereā€™s a cached version of the returned API data in your ā€œapiā€-cache under the ā€œapiDataā€-key.

So if youā€™ld like to remove the cached apiData from your ā€œapiā€-cache, youā€™ld have to do it like $apiCache->remove('apiData').

1 Like

Hey,
its a good answer, because it clarifies my question, although it does not solve it.
What you posted is found in the docs and is clear to me.

What I mean, on the other side, is a cache for the internal rest api: https://getkirby.com/docs/reference/api/pages
If there is something pre-build or if I have to build it on my own (Maybe hooking into route: and page.update:?), maybe somebody has a code snippet?

Thx!

i do not know of any cache for the core api. but maybe @rasteiner or @rbnschlz know some hidden gems since they do a lot api stuff.

Oh, Iā€™m sorry. I missed that you were referring to Kirbyā€™s own API.

I donā€™t know if thereā€™s anything built-in tbh. But if there isnā€™t, I think you should be able to get around that by setting it up as in the docs and consuming the siteā€™s own API (instead of an external API)?

that would make it public. the core api is protected by authentification.

are there any new information about it?
We should cache the api calls because we have built an react frontend with kirby as the cms. We have a lot of traffic and we do not want to send the from the frontendā€¦ this is a huge security issue, if you donā€™t protect the panel.