Kirby Time Cache - v0.2


#1

Github: https://github.com/jenstornell/kirby-partial-cache

I needed a simple way to use partial cache and at the same time have an expire time for the cached blocks.

I started out with a great little script made by @bnomei, which then became a good inspiration for this plugin: Cache part of a page? Thank you, I added you as a contributor in the repo.

Anyway, it’s really simple:

It will write “Hello world” on the screen and the second time it will load it from a cached file, if the expire time is not reached.

Expire time can be set as an option or an argument, default is 1 day

echo partial::cache('filename.json', function() {
    return 'Hello world!';
});

You can find more advanced examples in the repo.

Features

  • Cache anything you want.
  • Set the expire time as config option or as argument.
  • Set the cache folder to anything you like.
  • Hash the cache filename or not, the choice is yours.
  • Debug the cached blocks by using a HTML comment before and after.
  • Multiple instances. Multiple cache files, for multiple blocks, with different expire times.

#2

@jenstornell Man! You really love making plugins. :slight_smile:

Forgive me but can you please give a real world example why this is useful?


#3

In my case I update one of my sites with a part in the sidebar. That part is really slow because it get 30K products and kind of group them by categories.

One idea could be to use the Kirby cache for this, but I want to keep the rest of the content fresh. That sidebar part will not update very often so it’s ok if it’s only updated 1 time every day.

Most of the site now use a database and less of the site uses Kirby pages.

So it can be useful if a part of a site is really slow and you have reasons to not use the Kirby cache.


#4

Version 0.2 released!

Github: https://github.com/jenstornell/kirby-time-cache

Yes, I’ve changed the name from Kirby Partial Cache to Kirby Time Cache, because it’s not just for parts anymore.

I’ve been thinking a long time if I should continue the development of this plugin or not. Kirby 3 will probably be released this year and it makes uncertain of which plugins that will still be relevant in the future.

I’ve decided that this plugin will still have a place, now and in the future. Instead of shutting it down, I’ve improved it.

Caches full pages - With a refresh after a time expire

The biggest change with this version is that it caches full pages, similar to the built in cache. The difference is that it expires after a time of your choice. It’s especially good when you for example use a database where Kirby does not know when the content has been updated.

0.2

  • Changed name from Kirby Partial Cache to Kirby Time Cache.
  • New feature - Full pages caching.
  • Bug fix with filenames without dots.
  • Added new option time.cache.path, the path where the cache files will be stored.
  • Added new option time.cache.pages, to enable/disable caching of full pages.
  • Added new option time.cache.pages.expire, which is the expire time of cached pages.
  • Added new option time.cache.pages.comments, to enable/disable html comments of cached pages.
  • Removed site option ``.
  • Removed the options partial.cache.before, partial.cache.after and partial.cache.comments.
  • Changed license to MIT.

#5

Is there still the need for this plugin with v3? Browsing through the Caching section of the Guide, I get the feeling I’d just implement my own cache for stuff like … caching weather data and keeping it around for like 12h, thus saving requests … or am I mistaken?


#6

My plugin was written to cache parts of a page or full pages and with a timer refresh that particular part or page. I don’t think that you can do exactly that with the built in Kirby cache.

However if you can do what you need with the built in cache, then I think it’s better to not rely on a plugin.


#7

Maybe @texnixe knows more about that …

The guide states that “(y)ou can define how long a cache variable is valid”, and if being independent from page cache is in fact true …


#8

Yes, you can setup your own cache instance that is independent from the pages cache. That means, that if the pages cache is flushed, your own cache instance stays intact.

However, if you also use the pages cache on your page that displays your weather data, this data will probably not be updated even if you fetch new data from the API, unless you also flush the pages cache when you get new data from the Api or ignore those pages in your config settings.


#9

i took jens idea and created something similar for k3 during its beta. https://github.com/bnomei/kirby3-lapse