asm89/twig-cache-extension 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

asm89/twig-cache-extension

最新稳定版本:1.4.0

Composer 安装命令:

composer require asm89/twig-cache-extension

包简介

Cache fragments of templates directly within Twig.

README 文档

README

This extension moved to the Twig organization. Check out the repository over there instead: https://github.com/twigphp/twig-cache-extension.

The missing cache extension for Twig. The extension allows for caching rendered parts of templates using several cache strategies.

Build Status

Installation

The extension is installable via composer:

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

Quick start

Setup

A minimal setup for adding the extension with the LifeTimeCacheStrategy and doctrine array cache is as following:

<?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);

Want to use a PSR-6 cache pool?

Instead of using the default DoctrineCacheAdapter the extension also has a PSR-6 compatible adapter. You need to instantiate one of the cache pool implementations as can be found on: http://php-cache.readthedocs.io/en/latest/

Example: Making use of the ApcuCachePool via the PsrCacheAdapter:

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);

Usage

To cache a part of a template in Twig surround the code with a cache block. The cache block takes two parameters, first an "annotation" part, second the "value" the cache strategy can work with. Example:

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

Cache blocks can be nested:

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

The annotation can also be an expression:

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

Cache strategies

The extension ships with a few cache strategies out of the box. Setup and usage of all of them is described below.

Lifetime

See the "Quick start" for usage information of the LifetimeCacheStrategy.

Generational

Strategy for generational caching.

In theory the strategy only saves fragments to the cache with infinite lifetime. The key of the strategy lies in the fact that the keys for blocks will change as the value for which the key is generated changes.

For example: entities containing a last update time, would include a timestamp in the key. For an interesting blog post about this type of caching see: http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works

Blackhole

Strategy for development mode.

In development mode it often not very useful to cache fragments. The blackhole strategy provides an easy way to not cache anything it all. It always generates a new key and does not fetch or save any fragments.

Setup

In order to use the strategy you need to setup a KeyGenerator class that is able to generate a cache key for a given value.

The following naive example always assumes the value is an object with the methods getId() and getUpdatedAt() method. The key then composed from the class name, the id and the updated time of the object:

<?php

use Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface;

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

}

Next the GenerationalCacheStrategy needs to be setup with the keygenerator.

<?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);

Usage

The strategy expects an object as value for determining the cache key of the block:

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

Using multiple strategies

Different cache strategies are useful for different usecases. It is possible to mix multiple strategies in an application with the IndexedChainingCacheStrategy. The strategy takes an array of 'name' => $strategy and delegates the caching to the appropriate strategy.

Setup

<?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);

Usage

The strategy expects an array with as key the name of the strategy which it needs to delegate to and as value the appropriate value for the delegated strategy.

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

Implementing a cache strategy

Creating separate caches for different access levels, languages or other usecases can be done by implementing a custom cache strategy. In order to do so implement the CacheProviderInterface. It is recommended to use composition and wrap a custom strategy around an existing one.

Authors

Alexander iam.asm89@gmail.com

License

twig-cache-extension is licensed under the MIT License - see the LICENSE file for details

asm89/twig-cache-extension 适用场景与选型建议

asm89/twig-cache-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.73M 次下载、GitHub Stars 达 403, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「twig」 「cache」 「extension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 asm89/twig-cache-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 asm89/twig-cache-extension 我们能提供哪些服务?
定制开发 / 二次开发

基于 asm89/twig-cache-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.73M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 405
  • 点击次数: 18
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 403
  • Watchers: 24
  • Forks: 30
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04