Skip to content

Kirby 3.5.7.1

Caching

Caching pages

You can enable Kirby's page cache in your config.php file with the cache.pages option:

/site/config/config.php
return [
  'cache' => [
      'pages' => [
          'active' => true,
          'ignore' => function ($page) {
            return $page->title()->value() === 'Do not cache me';
          }
      ]
  ]
];

The page cache files are stored in the /site/cache folder.
Page requests will only be cached if there are no params in the URL.

Options

Option Value
active true/false
ignore Either an array of page IDs to ignore, or a callback function that returns a boolean

Cache drivers and options

Using a different cache driver

/site/config/config.php
return [
  'cache' => [
    'pages' => [
      'active' => true,
      'type'   => 'memcached',
      'host'   => 'localhost',
      'port'   => '11211',
      'prefix'  => 'pages',
      'ignore' => function ($page) {
        return $page->id() === 'something';
      }
    ]
  ]
];

Set up your own cache

If you want to create your own cache instance, you can just add it to the config like this:

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

That's it! Now you got a new cache up and running with the id api.

This will again be a file cache instance and stores the cache files in /site/cache/api.

You have the same extended options if needed and you can combine different cache types without hassle.

Using the cache

As soon as you configured your cache in the config, you can start using it like this (for example in a controller):

/site/controllers/your-api.php
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->content());
  }

  return compact('apiData');

};

You can define how long a cache variable is valid by specifying the number of minutes until it expires in the set method:

$apiCache->set('apiData', $apiData->content(), 30);

The getter will now return null after the 30 minutes expired.

Plugin caches

Plugins can have their own namespaced caches by defining them in their options:

Kirby::plugin('superwoman/superplugin', [
  'options' => [
    'cache' => true
  ]
]);

This plugin cache instance will be available at:

$kirby->cache('superwoman.superplugin');

But you can even have multiple cache instances per plugin:

Kirby::plugin('superwoman/superplugin', [
  'options' => [
    'cache.api' => true
  ]
]);

Following the same concept, you can access it like this:

$kirby->cache('superwoman.superplugin.api');