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

Issue when reading cache #25

Closed isaactopo closed 4 months ago

isaactopo commented 5 months ago

the plugin creates the cache file fine but the second time it reads the file it gives an error. I can't see the error as the Debug mode is disabled (which is when the cache works).

I'm caching a product collection:

$products = lapse(
    'cache1',
    function () use ($page) {
        return $page->children()->sortBy('pvp','desc');
    }
);

Am I doing something wrong?

I'm using version 4.1.0 over Kirby 4.3.0 on a MAMP PHP 8.3.0 Thank you very much

bnomei commented 4 months ago

lapse is intended to store primitive types like strings, numbers and arrays. in your example you are trying to serialize a full kirby cms collection object (Pages).

you could do something like this (untested):

$productIds = lapse(
    'cache1',
    function () use ($page) {
        return $page->children()->sortBy('pvp','desc')->map(fn($p) => $p->id())->values() ;
    }
);
$products = new \Kirby\Cms\Pages($productIds);
isaactopo commented 4 months ago

In the meantime I have been successfully testing with arrays. The code you provide works correctly but I'm not sure if it's an advantage. Probably the best thing to do is to activate Kirby's cache (and cache the pages with the collections). And also use Lapse to cache the Query's for Categories and Brands (which are arrays and are working fine for me at the moment). I have a CSV with products that I import and it creates about 1400 pages at the moment, would you do it any different? Thank you very much for the plugin, for your time, the clarification and the example!

bnomei commented 4 months ago

you are right. the example does not make much sense as the pages collection only gets sorted but not for example filtered or other queries executed. it will not yield any performance gain over the plain collections.

using the pages cache or even the staticache plugin will help a lot. initially i created lapse for those projects where these caches do not apply, like when you check for active users sessions etc.

if your total products count is about 1400 you should definitely notice a performance gain when caching the results of aggregated categories and brands. be mindful the cache is only active if the global debug mode is OFF.

I am glad to hear my plugin is of use to you. :)

isaactopo commented 4 months ago

Great, yes it's working great caching results performed by queries (categories,brands…) over the thousands products. I have been looking into Janitor and I see that I can generate the images before they load when making the request. I think that by caching the pages + Janitor, the page will work quite well. Thanks again.