Can I prevent clearing of the cache when page is saved?

I’m using the Kirby cache but in an own way where I want to trigger it when I want and not in other cases.

When I save a page the cache is cleared. Can I disable this feature?

I’m using this solution at the moment:

Short answer: No.

Long answer: A cache is only meant to contain information where it doesn’t matter if it gets deleted, i.e. information that is also stored elsewhere.
Not clearing the cache would only improve performance in these cases, but would lead to a lot of issues with old and invalid caches.

In my case I have 20 pages that fetches data from an external API (another of my Kirby sites).

To get data, save it and parse it takes a long time, even for a single page. To wipe out the cache for 20 pages and make these 20 pages rebuild the cache every time is not a very nice option for me. Maybe I want to save a page in the external API 10 times. Then I only want to reload the cache for that particular page 10 times, not for every page on the site.

That’s why my the part of a page cache thing works very well. When new data arrives it triggers a recache of the page that contains the new data.

I’ll think I try the soluition by @bnomei, it’s similar but disconnected to Kirby cache.

Suggestions:

Thanks! :slight_smile:

1 Like

@bnomei’s solution won’t actually help you here. Clearing the cache means deleting all files in the cache dir, no matter how they got there.

What you could though is to create your own cache driver based on the file cache driver and modify its flush method to make it only delete all cache files that don’t start with the prefix for your custom caching feature. Please note that this is a hack and not intended to work that way.

Oh, thanks for that. I’ll think I’ll still use a modified version of the @bnomei solution and just change the path:

$path = kirby()->roots()->index() . DS . 'storage' . DS . 'slug.json';

The final and working result looks like this:

It’s a controller:

return function($site, $pages, $page) {
  $localfile = kirby()->roots()->index() . DS . 'storage' . DS . $page->slug() . '.json';

  if( ! f::exists($localfile) ) {
    $local = array('foo' => 'bar');
    f::write( $localfile, json_encode( $local ) );
  } else {
    $local = json_decode( f::read( $localfile ), true );
  }

  return array(
    'local' => $local,
  );
};

Still really simple and I have total controll over this cache. Thanks @bnomei for the example and to @lukasbestle for support.

Bringing up an old topic here – I’d love to have the option of disabling the the cache clearing automatically.

I’ve recently started using Kirby as a CMS and API: delivering content as JSON with custom routes. I’m caching most of these calls, and selectively clearing them when my users update content.

In my current project, I’ve ‘solved’ this by hacking the core cache driver in the toolkit:

  public function flush($force = false) {
    if ($force) return dir::clean($this->options['root']);
  }

then I can manually flush the cache with a route:

  array(
    'method' => 'GET',
    'pattern' => 'api/flushcache',
    'action' => function() {
      $cleared = kirby()->cache()->flush(true);
      return response::json(json_encode($cleared));
    }
  ),

Like this, it’s working wonderfully. Of course, I’ve had to ensure that particular cache items are cleared.

Or, is there a way to create and use a separate cache that is not cleared by the panel?