yfktn/twigcacheplugin

October CMS 插件,用于将 Twig 缓存扩展添加到 OctoberCMS。

安装: 14

依赖项: 0

建议者: 0

安全: 0

星级: 1

关注者: 1

分支: 1

公开问题: 1

类型:october-plugin

v0.1.2 2021-06-13 12:18 UTC

This package is auto-updated.

Last update: 2024-09-14 10:27:34 UTC


README

twig 缓存插件扩展添加到 OctoberCMS。

安装插件

从 v0.0.6 版本开始,我们可以通过 composer 安装插件。从 OctoberCMS 路径的根目录

$ composer require yfktn/twigcacheplugin

您需要使用 v0.1 版本来配合 OctoberCMS 版本 1.1。

如何使用

此插件目前实现了 IndexedChainingCacheStrategy(),使用 时间 (LifetimeCacheStrategy()) 和 模型 (GenerationalCacheStrategy()) 作为索引来访问,当我们需要实现相关策略时。

要使用它,在您的 twig 模板中,在您想要缓存的代码部分周围添加一个 cache 块。

比如说,我们有一个需要缓存的首页部分,它会在 120 秒后过期,那么我们的代码将是

{# add homepage annotation with time strategy, for 10 seconds #}
{% cache "homepage" {time: 10} %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

在另一种情况下,您有一个视图用于显示 Eloquent 数据库模型的详细信息,它会在 7200 秒或我们的模型更新时(由于缓存策略需要在您的模型中作为 Carbon 对象生成缓存键的 created_at 字段,如果不存在,您需要创建自定义 mutator)过期,那么我们的代码将是

{# add model annotation #}
{% set annotmodel = "model" ~ post.id %}
{% cache annotmodel {model: post} %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

黑洞缓存策略

当您处于开发模式时,您不需要使用 twig 加载之前生成的缓存来查看。在配置文件中,您可以将 blackholeCacheStrategyMode 的值更改为 true,此策略防止之前缓存的版本被渲染。

TrueblackholeCacheStrategyMode 的默认值,并且 您必须 在将插件部署到生产服务器时将其值设置为 false

有关 twig 缓存插件扩展的更多信息,请参阅twig 缓存插件扩展页面。

注意:我的插件还处于开发的早期阶段,请谨慎使用。