asm89/twig-cache-extension

此包已被废弃,不再维护。作者建议使用 twig/cache-extension 包。

直接在 Twig 中缓存模板片段。

1.4.0 2020-01-01 20:47 UTC

This package is auto-updated.

Last update: 2020-10-05 13:12:52 UTC


README

此扩展已移至 Twig 组织。请查看那里的存储库: https://github.com/twigphp/twig-cache-extension

Twig 缺少的缓存扩展。该扩展允许使用多种缓存策略来缓存渲染的模板部分。

Build Status

安装

此扩展可以通过 composer 安装。

{
    "require": {
        "asm89/twig-cache-extension": "~1.0"
    }
}

快速开始

设置

使用 LifeTimeCacheStrategy 和 doctrine 数组缓存添加扩展的最小设置如下:

<?php

use Doctrine\Common\Cache\ArrayCache;
use Asm89\Twig\CacheExtension\CacheProvider\DoctrineCacheAdapter;
use Asm89\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy;
use Asm89\Twig\CacheExtension\Extension as CacheExtension;

$cacheProvider  = new DoctrineCacheAdapter(new ArrayCache());
$cacheStrategy  = new LifetimeCacheStrategy($cacheProvider);
$cacheExtension = new CacheExtension($cacheStrategy);

$twig->addExtension($cacheExtension);

想要使用 PSR-6 缓存池?

除了使用默认的 DoctrineCacheAdapter,此扩展还有一个兼容 PSR-6 的适配器。您需要实例化可在以下位置找到的缓存池实现之一: http://php-cache.readthedocs.io/en/latest/

示例:通过 PsrCacheAdapter 使用 ApcuCachePool

composer require cache/apcu-adapter
<?php

use Asm89\Twig\CacheExtension\CacheProvider\PsrCacheAdapter;
use Asm89\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy;
use Asm89\Twig\CacheExtension\Extension as CacheExtension;
use Cache\Adapter\Apcu\ApcuCachePool;

$cacheProvider  = new PsrCacheAdapter(new ApcuCachePool());
$cacheStrategy  = new LifetimeCacheStrategy($cacheProvider);
$cacheExtension = new CacheExtension($cacheStrategy);

$twig->addExtension($cacheExtension);

用法

要在 Twig 中缓存模板的一部分,请用 cache 块包围代码。缓存块有两个参数,第一个是“注解”部分,第二个是缓存策略可以与之一起工作的“值”。示例

{% cache 'v1/summary' 900 %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

缓存块可以嵌套

{% cache 'v1' 900 %}
    {% for item in items %}
        {% cache 'v1' item %}
            {# ... #}
        {% endcache %}
    {% endfor %}
{% endcache %}

注解也可以是一个表达式

{% set version = 42 %}
{% cache 'hello_v' ~ version 900 %}
    Hello {{ name }}!
{% endcache %}

缓存策略

此扩展自带一些缓存策略。下面描述了它们的设置和用法。

有效期

请参阅"快速开始"部分以获取 LifetimeCacheStrategy 的使用信息。

代际

代际缓存的策略。

理论上,此策略仅在无限生命期内将片段保存到缓存中。策略的关键在于,随着生成键的值的更改,块的关键将发生变化。

例如:包含最后更新时间的实体,会在键中包含时间戳。有关此类缓存的有意思的博客文章,请参阅: http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works

黑洞

开发模式的策略。

在开发模式下,通常没有必要缓存片段。黑洞策略提供了一种简单的方法来完全不缓存任何内容。它始终生成新的键,不检索或保存任何片段。

设置

为了使用此策略,您需要设置一个KeyGenerator类,该类能够为给定的值生成一个缓存键。

以下简单的示例始终假设值是一个具有getId()getUpdatedAt()方法的对象。然后,键由类的名称、id和对象的更新时间组成。

<?php

use Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface;

class MyKeyGenerator implements KeyGeneratorInterface
{
    public function generateKey($value)
    {
        return get_class($value) . '_' . $value->getId() . '_' . $value->getUpdatedAt();
    }

}

接下来,需要使用keygenerator设置GenerationalCacheStrategy

<?php

use Asm89\Twig\CacheExtension\CacheStrategy\GenerationalCacheStrategy;
use Asm89\Twig\CacheExtension\Extension as CacheExtension;

$keyGenerator   = new MyKeyGenerator();
$cacheProvider  = /* see Quick start */;
$cacheStrategy  = new GenerationalCacheStrategy($cacheProvider, $keyGenerator, 0 /* = infinite lifetime */);
$cacheExtension = new CacheExtension($cacheStrategy);

$twig->addExtension($cacheExtension);

用法

该策略期望一个对象作为值,以确定块的缓存键。

{% cache 'v1/summary' item %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

使用多个策略

不同的缓存策略适用于不同的用例。可以使用IndexedChainingCacheStrategy在应用程序中混合多个策略。该策略接受一个包含'name' => $strategy的数组,并将缓存委托给适当的策略。

设置

<?php

use Asm89\Twig\CacheExtension\CacheStrategy\IndexedChainingCacheStrategy;
use Asm89\Twig\CacheExtension\Extension as CacheExtension;

$cacheStrategy  = new IndexedChainingCacheStrategy(array(
    'time' => $lifetimeCacheStrategy,
    'gen'  => $generationalCacheStrategy,
));
$cacheExtension = new CacheExtension($cacheStrategy);

$twig->addExtension($cacheExtension);

用法

该策略期望一个数组,其中键是策略的名称,该策略需要将其委托给,值是委托策略的适当值。

{# delegate to lifetime strategy #}
{% cache 'v1/summary' {time: 300} %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

{# delegate to generational strategy #}
{% cache 'v1/summary' {gen: item} %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

实现缓存策略

通过实现自定义缓存策略,可以为不同的访问级别、语言或其他用例创建单独的缓存。为了做到这一点,实现CacheProviderInterface。建议使用组合并将自定义策略包装在现有的策略中。

作者

Alexander iam.asm89@gmail.com

许可协议

twig-cache-extension在MIT许可协议下许可 - 详细信息请参阅LICENSE文件。