lbarulski / cache-tags-bundle
自定义Varnish标签的Bundle
v1.2.3
2022-10-04 06:51 UTC
Requires
- php: >=5.4
- doctrine/annotations: 1.*
- sensio/framework-extra-bundle: 3.0.*
- symfony/config: ^2.8|^3.0
- symfony/console: ^2.8|^3.0
- symfony/dependency-injection: ^2.8|^3.0
- symfony/http-kernel: ^2.8|^3.0
README
CacheTagsBundle是一个简单的Varnish Cache的缓存标签和缓存失效工具
Varnish VCL
sub vcl_recv {
if (req.request == "BAN") {
if (req.http.X-CACHE-TAG) {
ban("obj.http.X-CACHE-TAGS ~ " + req.http.X-CACHE-TAG);
} else {
error 400 "Tag not given";
}
error 200 "Banned";
}
}
安装
CacheTagsBundle库在Packagist上可用。您可以使用[Composer] (https://getcomposer.org.cn)进行安装
方法1
简单地运行,假设您已安装composer.phar或composer二进制文件
$ composer require lbarulski/cache-tags-bundle
方法2
- 在您的composer.json中添加以下行
{ "require": { "lbarulski/cache-tags-bundle": "2.0.x-dev" } }
- 运行Composer以下载Bundle
$ composer require 'lbarulski/cache-tags-bundle:2.0.x-dev'
将此Bundle添加到您的应用程序kernel中
// app/ApplicationKernel.php public function registerBundles() { return array( // ... new lbarulski\CacheTagsBundle\CacheTagsBundle(), // ... ); }
config.yml配置示例
# app/config/config.yml cache_tags: response: tag: X-CACHE-TAGS proxies: varnish: - { host: host.tld, port: 80, path: /, timeout: 1, header: X-CACHE-TAG, host_header: my.site.com } # For SSL - { host: ssl://host.tld, port: 443, path: /, timeout: 1, header: X-CACHE-TAG, host_header: my.site.com, ssl_verify_peer: true }
host_header
允许伪造请求主机头;可选,默认为host
值
使用示例
控制器:简单的缓存标签
// Acme\MainBundle\Controller\ArticleController.php use lbarulski\CacheTagsBundle\Annotation\CacheTag\Plain; ... /** * @CacheTag\Plain("article_name") **/ public function articleAction(Request $request) { ... $response = new Response('...'); $response->setPublic(); $response->setTtl(3600); return $response; }
控制器:请求属性标签
// Acme\MainBundle\Entity\Article.php use lbarulski\CacheTagsBundle\Tag\CacheTagInterface; class Article implements CacheTagInterface { ... public function getCacheTag() { return 'article_'.$this->getId(); } }
// Acme\MainBundle\Controller\ArticleController.php use lbarulski\CacheTagsBundle\Annotation\CacheTag\RequestAttribute; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; ... /** * @ParamConverter("article") * @CacheTag\RequestAttribute("article") **/ public function articleAction(Article $article) { ... $response = new Response('...'); $response->setPublic(); $response->setTtl(3600); return $response; }
视图模板:使用esi控制器
{{ render_esi(controller('MainBundle:Article:article', { article: article.id })}}
失效
命令 invalidate TAG
$ ./app/console cache_tags:invalidate tag
Invalidate TAG
// Acme\MainBundle\Controller\ArticleController.php use lbarulski\CacheTagsBundle\Tag\Plain; ... public function updateArticleAction(Article $article) { ... $tag = 'article_name'; $this->get('cache_tags.invalidator')->invalidate(new Plain($tag)); ... }
展示
http://www.slideshare.net/chylek/cachetagsbundle
许可证
此库根据MIT许可证发布。有关更多信息,请参阅包含的LICENSE文件。