krak / cache-buster
v0.1.1
2016-05-01 07:19 UTC
Requires
- guzzlehttp/streams: ^3.0
- symfony/console: ^2.0|^3.0
Requires (Dev)
- peridot-php/peridot: ^1.18
- symfony/asset: ^3.0
Suggests
- symony/asset: Allows nice integration with generating urls
This package is not auto-updated.
Last update: 2024-09-22 09:24:29 UTC
README
Cache Buster库用于清除更改的资产的缓存。
使用方法
缓存清除是一个两部分系统,其中bust-cache
程序将生成一个新文件信息的配置文件。然后您需要在代码库中利用这些文件信息。
命令行界面
使用./bin/bust-cache
获取缓存清除的使用界面和帮助。
此外,您还可以在您的控制台应用程序中注册Krak\CacheBuster\Command\BustCacheCommand
,如下所示。
<?php
// app is an instance of Symfony Console Application
$app->addCommand(new Krak\CacheBuster\Command\BustCacheCommand());
代码
Cache Buster提供了一个CacheBusterVersionStrategy
以集成到Symfony的Asset组件。
您只需传入配置文件的路径。
<?php
use Krak\CacheBuster\Asset\CacheBusterVersionStrategy,
Symfony\Component\Asset\Package;
$config = $is_prd_environment ? require __DIR__ . '/path/to/config.php' : [];
$package = new Package(new CacheBusterVersionStrategy($config));
echo $package->getUrl('/project.js');
// outputs the cache-busted file name
注意:对于可能不使用缓存清除的dev环境,只需传入一个空数组,这将始终触发装饰的版本策略实例。
Symfony资产集成
如果您已经在项目中使用了Symfony Asset库,那么您可以传递一个版本策略到CacheBusterVersionStrategy
的第二个参数,以装饰您的版本策略。
<?php
use Krak\CacheBuster\Asset\CacheBusterVersionStrategy,
Symfony\Component\Asset;
$vs = new Asset\VersionStrategy\StaticVersionStrategy('v1');
$package = new Package(new CacheBusterVersionStrategy($config, $vs));
然后任何不在缓存清除配置中的资源/文件将通过包装的版本策略实例直接通过。如果没有使用版本策略,它将默认使用EmptyVersionStrategy
。