monolyth/cachet
Composer 安装命令:
composer require monolyth/cachet
包简介
Cachebuster command
README 文档
README
Cachebuster command
When serving static assets (mainly JavaScript and CSS, but perhaps also
images...) sometimes you'll want to "bust" the browser cache to make sure
visitors receive the correct version. This is what cachet aims to help in.
Installation
Composer (recommended):
$ composer require monolyth/cachet
Usage
cachet depends on a global "versions file" where all current versions are
stored. This should contain a JSON hash of all files/hashes (relative to your
projet's root directory). Initially you can fill it with empty strings as the
hashes; on first run, the actual hashes will be replaced:
{"some/file/somewhere.js":""}
Whenever you run the cachet CLI command, the current hashes will be inserted
or updated (based on the files' contents):
$ vendor/bin/cliff cachet path/to/versions.json public/folder
The command also creates symlinks with the correct hash inserted into the public
folder, e.g. test.js might become test.abcdabcd.js. You should refer to
these linked files in your frontend code, as the hash will change when the file
contents have changed.
Note that both arguments are relative to the current working directory.
Injecting version numbers in templates
Monolyth projects usually use Twig, so we've included a TwigFunction cachet
to easily inject the correct version numbers.
Somewhere in a central place in your application, inject it using your
Twig\Enviroment of choice:
<?php // For example: $twig = new Twig\Environment(new Twig\Loader\FilesystemLoader(__DIR__)); //...your other Twig stuff... Monolyth\Cachet\Twig::inject('path/to/version/file.json', $twig);
Note that when injecting the Twig environment, you need to pass the full path
to the versions file since getcwd() has an undefined meaning here.
In your Twig template, pass the filename to be cache-busted through the cachet
function:
<script src="/{{ cachet('js/test.js') }}"></script>
An optional second parameter to the cachet function may be set to false to
disable all cache-busting; this is handy during development. You'll probably
already have a way to determine whether or not the code is running in
production. A fake example:
<script src="/{{ cachet('js/test.js', isProd()) }}"></script>
Note, by the way, the leading slash in the src attribute.
Fault tolerance
The Twig cachet function returns the original filename if no such "bustable"
file was defined. This is the same behaviour as when the second argument is
false.
If E_NOTICE level errors are enabled though (a good idea during development
and testing) an error of level E_USER_NOTICE will be triggered.
monolyth/cachet 适用场景与选型建议
monolyth/cachet 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 142 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 monolyth/cachet 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 monolyth/cachet 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 142
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-28