Skip to content

bnomei/kirby-nitro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ab9c89d · Aug 10, 2024

History

26 Commits
Jul 22, 2024
Aug 10, 2024
Aug 10, 2024
Aug 10, 2024
Jul 18, 2024
Jul 18, 2024
Jul 21, 2024
Jul 18, 2024
Aug 10, 2024
Aug 10, 2024
Aug 10, 2024
Aug 10, 2024
Jul 21, 2024
Jul 21, 2024
Jul 18, 2024
Jul 21, 2024

Repository files navigation

⛽️ Kirby Nitro

Release Downloads Coverage Maintainability Discord

Nitro speeds up the loading of content in your Kirby project.

Commercial Usage


Support open source!

This plugin is free but if you use it in a commercial project please consider to sponsor me or make a donation.
If my work helped you to make some cash it seems fair to me that I might get a little reward as well, right?

Be kind. Share a little. Thanks.

‐ Bruno
 
M O N E Y
Github sponsor Patreon Buy Me a Coffee Paypal dontation Hire me

Installation

  • unzip master.zip as folder site/plugins/kirby-nitro or
  • git submodule add https://github.com/bnomei/kirby-nitro.git site/plugins/kirby-nitro or
  • composer require bnomei/kirby-nitro

Checklist: When to use this plugin?

  • You load more than 100 but less than 2000 models (pages/files/users) in a single request?
  • You have less than 4000 models or 2 MB combined TXT files in your project?
  • If you load less, you do not need any performance plugins apart from maybe a key-value caching helper.
  • If you load more, you should consider Boost or Khulan instead.
  • If you need to process multiple requests fully concurrently you should not use this plugin. But from my experience most Kirby projects do not need that.

Global & Atomic Cache

The Nitro cache is a global cache. This means that the cache is shared between all HTTP_HOST environments. This will make it behave like a single database connection.

The Nitro cache is by default an atomic cache. This means that the cache will block the cache file for the full duration of your request to maintain data consistency. This will make it behave like a database with locks.

Warning

No matter how many php-fpm workers you have, only one will be running at a time when Nitro is in atomic mode! You have been warned! But this is the only way to guarantee data consistency, and it will still be wicked fast.

Setup

For each template you want to be cached you need to use a model to add the content cache logic using a trait.

site/models/default.php

class DefaultPage extends \Kirby\Cms\Page
{
    use \Bnomei\ModelWithNitro;
}

or

site/models/article.php

class ArticlePage extends \Kirby\Cms\Page
{
    use \Bnomei\ModelWithNitro;
}

Note

You can also use the trait for user models. File models are patched automatically.

Using the Cache

You can use the single-file-based cache of Nitro to store your own key-value pairs, just like with a regular cache in Kirby.

nitro()->cache()->set('mykey', 'value');
nitro()->cache()->set('mykey', 'value', 1);

$value = nitro()->cache()->get('mykey');
$value = nitro()->cache()->getOrSet('mykey', fn() => 'value');

The Nitro cache is a bit smarter than the default cache in Kirby. It allows you optionally provide keys as arrays, it will serialize values automatically (like Kirby fields to their ->value()) and storing a value can be canceled.

nitro()->cache()->set(['articles', $page->slug()], $page->title());

nitro()->cache()->set('test', function () {
    // ... some logic
    if($cancel) {
        throw new \Bnomei\Nitro\AbortCachingExeption();
    }
});

Warning

Since the Nitro cache is fully loaded with every request I would not advise to store too many big chunks of data (like HTML output or when having too many models in total).

Using the Cache Driver in Kirby

You can also use the singe-file-based cache of Nitro as a cache driver for Kirby. This will allow you to use it for caching of other extensions in Kirby.

Note

I would highly recommend to use the Nitro cache for Kirby's UUID cache.

site/config/config.php

return [
    // ... other options
    
    // use nitro as cache driver for storing uuids
    // instead of the default file-based cache
    'cache' => [
        'uuid' => [
            'type' => 'nitro',
        ],
    ],
    
    // example: in Lapse plugin
    'bnomei.lapse.cache' => [
        'type' => 'nitro',
    ],
];

Settings

bnomei.nitro. Default Description
global true all HTTP_HOSTs will share the same cache
atomic true will lock the cache while a request is processed to achieve data consistency
sleep 1000 duration in MICRO seconds before checking the lock again
auto-clean-cache true will clean the cache once before the first get()
patch-dir-class always on monkey-patch the \Kirby\Filesystem\Dir class to use Nitro for caching
patch-files-class true monkey-patch the \Kirby\CMS\Files class to use Nitro for caching its content
max-dirty-cache 512 write every N changes or on destruct
json-encode-flags JSON_THROW_ON_ERROR
model.read true read from cache for all models that use the ModelWithNitro trait
model.write true write to cache for all models that use the ModelWithNitro trait

Disclaimer

This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.