timnarr/kirby-snippet-cache

此插件为 Kirby CMS 片段提供缓存功能。

0.1.0 2023-11-10 14:38 UTC

This package is auto-updated.

Last update: 2024-09-10 16:34:01 UTC


README

此插件通过 snippet-cache 片段为 Kirby CMS 片段输出提供缓存功能。

如果您不能使用 Kirby 的整体页面缓存,但是有进行大量查询或循环并转换大量数据的片段,这可能很有用。

安装

下载

下载并将此存储库复制到 /site/plugins/kirby-snippet-cache

Composer

composer require timnarr/kirby-snippet-cache

用法

// 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']]) ?>

没有使用插槽的用法

⚠️ 很遗憾,传递插槽到缓存片段不支持,并且可能永远不会支持。

以下是一个带有插槽的缓存片段的示例。这将缓存片段输出,但每次都会执行插槽中的 PHP 代码,如果您在这里进行大量任务(这里使用 sleep(4) 作为示例),这不会提供任何性能优势。

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

清除缓存

当发生网站(使用 page.update:after 钩子)或页面更新(使用 site.update:after 钩子)时,所有缓存都会自动清除。

选项

在您的 config.php 文件中设置选项

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

许可证

MIT License 版权所有 © 2023 Tim Narr