定制 bnomei/kirby-nitro 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bnomei/kirby-nitro

最新稳定版本:2.1.1

Composer 安装命令:

composer require bnomei/kirby-nitro

包简介

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

README 文档

README

ARCHIVED MAY 2025. suggested alternative: https://github.com/bnomei/kirby-turbo

⛽️ Kirby Nitro

Release Discord Buymecoffee

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

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.

Usecase

The plugin will speed up Kirby setups, loading 100-2000 page models in a single request by providing a special cache. It solves the three major performance bottlenecks in Kirby that I know of and links the cache between CLI and HTTP requests.

It does this by:

  • Providing a regular file cache you can use yourself. But the cache is always fully loaded in every request and only uses a single file, which makes it wicked fast.
  • Optionally you can use that cache for storing the UUID to page ID relations. So instead of loading a single file for every page model UUID resolution, the cache will have them ready instantly.
  • It allows you to store the TXT content of selected page/file/user models in the cache to speed up the loading time of content.
  • I uses a second cache, which will cache Dir::index results. This will skip crawling the file structure step in populating the full index from the cache until you update any page/file.

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-unlock-cache true will forcibly unlock the cache if it could not get a lock within set time
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.

bnomei/kirby-nitro 适用场景与选型建议

bnomei/kirby-nitro 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 9, 最近一次更新时间为 2024 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「performance」 「page」 「file」 「cache」 「user」 「model」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bnomei/kirby-nitro 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bnomei/kirby-nitro 我们能提供哪些服务?
定制开发 / 二次开发

基于 bnomei/kirby-nitro 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-19