bnomei / kirby3-lapse

Cache any data until set expiration time
https://forum.getkirby.com/t/kirby3-lapse-cache-any-data-until-set-expiration-time-with-automatic-keys/23586
MIT License
20 stars 0 forks source link

not working #8

Closed S1SYPHOS closed 4 years ago

S1SYPHOS commented 4 years ago

Hey there, as I'd like to use this plugin, I don't seem to be able to make it work as expected. i got the following setup to try caching some results, fetched from my package.json:

Controller:

<?php

use Bnomei\Lapse;

return function ($kirby, $page) {

    $key = crc32($page->url());

    $expires = 5;

    $data = function () use ($kirby) {
        $file = F::read($kirby->root('base') . '/package.json');
        $content = json_decode($file, true);

        return $content['dependencies'];
    };

    $data = Lapse::io($key, $data, $expires);

    return compact(
        'data',
    );
};

Template:

<ul>
    <?php foreach ($shit as $library => $version) : ?>
    <li><?= $library ?> is version <?= $version ?></li>
    <?php endforeach ?>
</ul>

.. but changing version numbers immediately gets shown where I expected a cached version to be delivered. Or maybe I got the concept wrong?

Any pointer in the right direction is highly appreciated.

bnomei commented 4 years ago

@S1SYPHOS sorry to respond so late. feel free to ping me on slack if i ever miss an issue again.

are you on debug mode => true? then the plugin will not cache anything – but wipe the cache and do not create new caches.

bnomei commented 4 years ago

based on you code i doubt that might be happening (since you use the url) but it could also be that you use the same key somewhere else an thus overwrite the result.

the plugin can take care of the key automatically if you want to reduce it to a one-liner.

    $data = Lapse::io($page, function () {
        $file = F::read(kirby()->root('base') . '/package.json');
        $content = json_decode($file, true);

        return $content['dependencies'];
    }, 5);

another good tip is to add any object you use() to the keys array as well. then the automatic key will create different caches when the other object changes.

$flag = get('file', 'package');
$data = Lapse::io([$page, $flag], function () use ($flag) {
    $file = F::read(kirby()->root('base') . '/'.$flag.'.json');
S1SYPHOS commented 4 years ago

will check that, thx!