Reading from plugin cache

Hey! I got a question about caching.

I have a collection of around 200 pages. The pages contain a field year. I extract that value via $years = $kirby->collection("projects")->pluck("year", ",", true) and create a navigation afterwards.

As I use that navigation on almost every page, I thought it would be best to create a plugin cache and store those values instead of extracting them from the collection on every occasion. I did something similar as @bnomei suggested in this thread: Increase performance of query - #3 by bnomei

// plugin cache

Kirby::plugin('sr/custom-cache', [
  'options' => ['cache' => true],
  'hooks' => [
    'page.update:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
      if($newPage->template()->name() == 'project') {
        $y =  $kirby->collection("projects")->pluck("year", ",", true);
        $years = kirby()->cache('sr.custom-cache')->set('years', $y);
      }
    }
  ]
]);
// controllers/site.php

$years = kirby()->cache('sr.custom-cache')->get('years');

// Build cache if empty
if($years == null) {
    $y =  $kirby->collection("projects")->pluck("year", ",", true);
    $years = kirby()->cache('sr.custom-cache')->set('years', $y);
}
<!-- nav snippet -->
<ul>
<?php foreach( $years as $year ) : ?>
    <li><a href="<?= $site->url() . '/' . $year ?>"><?= $year ?></a></li>
<?php endforeach; ?>
</ul>

Everything works fine so far: The plugin cache is created and only rebuilt if I update a project page. The created file is rather small and just contains an array with approx. 20 numbers/years.

What puzzled me was when I measured the loading time: It added 0.2s to every page containing the navigation.

f.ex. pluck vs. reading from cache: 0.02s vs. 0.22s, 0.7s vs. 0.9s, etc.

How is extracting values from a collection faster than reading an array from a file? Does that method only pay off if there are a lot of pages in a collection? Or have I done something wrong?

0.2s to load an array of 20 numbers from cache is suspiciously slow.

I mean, plucking through 200 pages would probably be still acceptable without cache, but having the cache definitely shouldn’t make it slower.

Can you try something like this to verify if it actually is the cache?

// controllers/site.php

$start = microtime(true);
$years = kirby()->cache('sr.custom-cache')->get('years');
$deltams = (microtime(true) - $start) * 1000;
header("Server-Timing: cacher;desc=\"Cache Read\";dur=$deltams", false);

// Build cache if empty
if($years == null) {
    $start = microtime(true);
    $y =  $kirby->collection("projects")->pluck("year", ",", true);
    $years = kirby()->cache('sr.custom-cache')->set('years', $y);
    $deltams = (microtime(true) - $start) * 1000;
    header("Server-Timing: cachew;desc=\"Cache Write\";dur=$deltams", false);
}

Then open the page and in the browsers developer tool look for the server timing. E.g. in Chrome you’d find this under the network tab, then select the request to the page, then select the “Timing” tab inside of the drawer that opened, then scroll down to find the “Server Timing”.
There you should see how much time reading from (and optionally writing to) the cache took.

Well… fooled myself on this one.

Before I started the timer above the <html> tag in the template.

When I wanted to verify that $years is actually read from cache, as you suggested, I used the same starting point for the page loading time. After passing it down to the template the time evened out…

So it’s basically the same loading time for both methods: 0.2s vs. 0.2s, 0.9s vs 0.9s and so on… Maybe with a slight edge of a few milliseconds to the cached version which makes more sense.

Sorry for the buzz!