emanueleminotto/twig-cache-bundle

Symfony 2 Bundle for twigphp/twig-cache-extension

1.3.1 2021-11-09 13:47 UTC

This package is auto-updated.

Last update: 2024-09-09 19:41:13 UTC


README

Build Status SensioLabs Insight Coverage Status Scrutinizer Code Quality Total Downloads

Symfony Bundle for twigphp/twig-cache-extension.

API: emanueleminotto.github.io/TwigCacheBundle

使用Composer安装Bundle

打开命令行控制台,进入您的项目目录,然后执行以下命令以下载此Bundle的最新稳定版本

$ composer require emanueleminotto/twig-cache-bundle

此命令要求您全局安装了Composer,如Composer文档中的安装章节所述。

将Bundle添加到AppKernel

然后,通过在您的项目bundles.php文件中添加以下行来启用Bundle

// bundles.php
return [
    // ...
    EmanueleMinotto\TwigCacheBundle\TwigCacheBundle::class => ['all' => true],
    // ...
];

配置服务

此Bundle允许您轻松配置扩展将用于缓存的缓存服务。默认情况下,扩展支持Doctrine\Common\Cache\Cache的实例,但允许您使用提供psr/cache-implementation的包。

# app/config/config.yml
twig_cache:
    service: cache_service # instance of Doctrine\Common\Cache\Cache or Psr\Cache\CacheItemPoolInterface

通过将扩展的默认适配器类更改为提供的PsrCacheAdapter,可以配置PSR-6 Cache pool实现。

# parameters.yml
twig_cache.adapter.class: Twig\CacheExtension\CacheProvider\PsrCacheAdapter

之后,您应该安装一个提供psr/cache-implementation的包。在http://php-cache.readthedocs.io/上可以找到大量实现。通过Composer安装适配器后,可以配置它为此Bundle执行缓存。该CacheBundle允许通过创建缓存池适配器的Symfony服务来轻松配置。

# config.yml
cache_adapter:
    providers:
        twig_apcu:
            factory: 'cache.factory.apcu'

twig_cache:
    service: cache.provider.twig_apcu

用法

默认策略是IndexedChainingCacheStrategy,因此您可以直接在您的Twig模板中使用此代码。

{# 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 %}

阅读

许可证

此Bundle采用MIT许可证。请参阅Bundle中的完整许可证。

Resources/meta/LICENSE