emanueleminotto / twig-cache-bundle
Symfony 2 Bundle for twigphp/twig-cache-extension
1.3.1
2021-11-09 13:47 UTC
Requires
- php: ^7.2|^8.0
- twig/cache-extension: ^1.4
Requires (Dev)
- doctrine/cache: ^1.4
- matthiasnoback/symfony-dependency-injection-test: ^4.2.0
- phpunit/phpunit: ^7.0|^8.0
- symfony/framework-bundle: ^4.0|^5.0
- symfony/twig-bundle: ^4.0|^5.0
Suggests
- cache/adapter-bundle: To make use of PSR-6 cache implementation via PsrCacheAdapter.
- cache/cache-bundle: To easily be able to configure adapters in your Symfony application.
- itinance/doctrine-cache-adapter-with-site-context: a Cache Adapter used to respect the current site context in a white-label/multi-site environment
README
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