定制 timnarr/kirby-snippet-cache 二次开发

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

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

timnarr/kirby-snippet-cache

最新稳定版本:0.1.0

Composer 安装命令:

composer require timnarr/kirby-snippet-cache

包简介

This plugin provides caching functionality for Kirby CMS snippets.

README 文档

README

This plugin provides caching functionality for Kirby CMS snippet output via a snippet-cache snippet.

It may be useful if you can't use Kirby's overall page cache, but have snippets that do extensive queries or loop and transform a lot of data.

Installation

Download

Download and copy this repository to /site/plugins/kirby-snippet-cache.

Composer

composer require timnarr/kirby-snippet-cache

Usage

// Load the `header` snippet with default cache duration
<?php snippet('snippet-cache', ['snippet' => 'header']) ?>

// Load the `blog/author` snippet with default cache duration
<?php snippet('snippet-cache', ['snippet' => 'blog/author']) ?>

// Load the `blog/author` snippet with a custom cache duration
<?php snippet('snippet-cache', ['snippet' => 'blog/author', 'duration' => 30]) ?>

// Load the `blog/author` snippet and pass variables to the cached snippet
<?php snippet('snippet-cache', ['snippet' => 'blog/author', 'variables' => ['author' => $author]]) ?>
<?php snippet('snippet-cache', ['snippet' => 'blog/author', 'variables' => compact('author')]) ?>

// You can also use alternative/fallback snippets
<?php snippet('snippet-cache', ['snippet' => ['articles/' . $page->postType(), 'articles/default']]) ?>

No usage with slots

⚠️ Unfortunately, passing slots to cached snippets is not supported and probably never will be.

Take a look at the following example of what a cached snippet with slots would look like. This would cache the snippet output, but also execute the slotted PHP each time, and if you are doing extensive tasks here (here using sleep(4) as an example), this would not provide any performance benefits.

<?php snippet('snippet-cache', ['snippet' => 'mySnippet'], slots: true) ?>
	<h1>This is the default slot title</h1>
	<?php sleep(4) ?>
<?php endsnippet() ?>

Clear cache

All caches are automatically cleared when an site (using the page.update:after hook) or page update (using the site.update:after hook) happens.

Options

Option Default Description
duration 0 Default cache duration in minutes. Can be defined per snippet. 0 means infinite cache duration.

Set options in your config.php file:

return [
	'cache' => [
		'timnarr.snippet-cache' => true,  // Enable snippet-cache...
		'pages' => [
			'active' => false // ... while deactivating page-cache
		]
	],
	'timnarr.snippet-cache' => [
		'duration' => 0 // in minutes
	],
];

License

MIT License Copyright © 2023 Tim Narr

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-10