定制 adriansuter/twig-cache-busting 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

adriansuter/twig-cache-busting

Composer 安装命令:

composer require adriansuter/twig-cache-busting

包简介

Twig Cache Busting is an add-on for Twig to support cache busting on template compilation.

README 文档

README

Build Status Coverage Status Total Downloads License

Twig Cache Busting is an add-on for Twig to support cache busting on template compilation.

Installation

$ composer require adriansuter/twig-cache-busting

Description

This add-on would extend Twig by a cache busting mechanism. The cache busting is taking place upon compilation of the template (not upon rendering). So whenever you update an asset, you would need to recompile the templates that reference this asset (or clear the cache and let Twig rebuild it automatically). The benefit is, that if you cache your compiled templates, then the server would process the performance intense cache busting only once (not on every request).

Cache Busters

By default, there are three cache busting methods. But you can develop your own custom cache buster by implementing the \AdrianSuter\TwigCacheBusting\Interfaces\CacheBusterInterface.

  • Query Param Cache Buster This cache buster would append a query param to the file path. So an asset with the path image.jpg gets transformed for example to image.jpg?c=abcd.
  • File Name Cache Buster This cache buster would alter the file name. So an asset with the path image.jpg gets transformed for example to image.abcd.jpg.
  • Dictionary Cache Buster This cache buster would use a lookup table (map) to find the target file path.

Hash Generators

The Query Param Cache Buster and the File Name Cache Buster both use a hash generator to generate a hash for the given asset (in the examples above, the hash is abcd). By default the following hash generators are possible. But you can develop your own custom hash generator by implementing \AdrianSuter\TwigCacheBusting\Interfaces\HashGeneratorInterface.

  • FileMD5HashGenerator This hash generator calculates the MD5 hash of the file.
  • FileSHA1HashGenerator This hash generator calculates the SHA1 hash of the file.
  • FileModificationTimeHashGenerator This hash generator uses the file modification time as Unix timestamp.

Usage

Query Param Cache Buster

Assume you have a file /home/htdocs/public/assets/image.jpg and your template is as follows:

<img src="{% cache_busting 'assets/image.jpg' %}">

To use the Query Param Cache Buster you need to pass the QueryParamCacheBuster to the static create method of the CacheBustingTwigExtension.

use AdrianSuter\TwigCacheBusting\CacheBusters\QueryParamCacheBuster;
use AdrianSuter\TwigCacheBusting\CacheBustingTwigExtension;

//...

$twig->addExtension(
    CacheBustingTwigExtension::create(
        new QueryParamCacheBuster('/home/htdocs/public')
    )
);

By default, the QueryParamCacheBuster uses the FileModificationTimeHashGenerator. But you can set another generator by passing a second argument to the constructor. For example:

use AdrianSuter\TwigCacheBusting\HashGenerators\FileMD5HashGenerator;

new QueryParamCacheBuster('/home/htdocs/public', new FileMD5HashGenerator())

File Name Cache Buster

Assume you have a file /home/htdocs/public/assets/image.jpg and your template is as follows:

<img src="{% cache_busting 'assets/image.jpg' %}">

To use the File Name Cache Buster you need to pass the FileNameCacheBuster to the static create method of the CacheBustingTwigExtension.

use AdrianSuter\TwigCacheBusting\CacheBusters\FileNameCacheBuster;
use AdrianSuter\TwigCacheBusting\CacheBustingTwigExtension;

// ...

$twig->addExtension(
    CacheBustingTwigExtension::create(
        new FileNameCacheBuster('/home/htdocs/public')
    )
);

Your web server needs to be configured such that the cache busting requests get redirected. For Apache you would need to set

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(\d+)\.(js|css|jpg|png|svg)$ $1.$3 [L]

Note that you might want to add more extensions to the rewrite rule.

By default, the FileNameCacheBuster uses the FileModificationTimeHashGenerator. But you can set another generator by passing a second argument to the constructor. For example:

use AdrianSuter\TwigCacheBusting\HashGenerators\FileMD5HashGenerator;

new FileNameCacheBuster('/home/htdocs/public', new FileMD5HashGenerator())

If your hash generator returns hexadecimal hashes, then you would need to adapt the Apache rewrite rule appropriately. For example:

RewriteRule ^(.+)\.([a-f0-9]+)\.(js|css|jpg|png)$ $1.$3 [L]

Dictionary Cache Buster

This cache busting method would use a dictionary to lookup file names. The dictionary is basically a mapping between the original file path and the cache busting version.

use AdrianSuter\TwigCacheBusting\CacheBusters\DictionaryCacheBuster;
use AdrianSuter\TwigCacheBusting\CacheBustingTwigExtension;
use AdrianSuter\TwigCacheBusting\Dictionaries\ArrayDictionary;

// ...

$twig->addExtension(
    CacheBustingTwigExtension::create(
		new DictionaryCacheBuster(
			new ArrayDictionary([
				'assets/image.jpg' => 'assets/cb-1c2d7c4s36d47d.jpg',
			])
		)
	)
);

Base Path

If you want to prepend a base path to the generated paths, then simply pass that to the static create method of the CacheBustingTwigExtension. For example:

$twig->addExtension(
    CacheBustingTwigExtension::create(
        new QueryParamCacheBuster('/home/htdocs/public'),
        'base-path'
    )
);

Custom Twig Tag

If you want to change the twig tag cache_busting into something else, you can do that simply by setting the third argument of the static create method of the CacheBustingTwigExtension. For example:

$twig->addExtension(
    CacheBustingTwigExtension::create(
        new QueryParamCacheBuster('/home/htdocs/public'),
        null,
        'cb'
    )
);

Now you can use cb in your templates in order to apply cache busting.

<img src="{% cb 'assets/image.jpg' %}">

Contribution

Is much welcomed.

adriansuter/twig-cache-busting 适用场景与选型建议

adriansuter/twig-cache-busting 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.34k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 09 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-18