CacheTagsBundle is simple Cache Tag and Cache Invalidation for Varnish Cache
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";
}
}
- PHP 5.3+
- Symfony2
The CacheTagsBundle library is available on [Packagist] (https://packagist.org/packages/lbarulski/cache-tags-bundle). You can install it using [Composer] (http://getcomposer.org):
Simply run assuming you have installed composer.phar or composer binary:
$ composer require lbarulski/cache-tags-bundle
- Add the following lines in your composer.json:
{
"require": {
"lbarulski/cache-tags-bundle": "dev-master"
}
}
- Run the composer to download the bundle
$ composer update lbarulski/cache-tags-bundle
// app/ApplicationKernel.php
public function registerBundles()
{
return array(
// ...
new lbarulski\CacheTagsBundle\CacheTagsBundle(),
// ...
);
}
// 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 }
// 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;
...
/**
* @ParamConverter("article")
* @CacheTag\RequestAttribute("article")
**/
public function articleAction(Article $article)
{
...
$response = new Response('...');
$response->setPublic();
$response->setTtl(3600);
return $response;
}
{{ render_esi(controller('MainBundle:Article:article', { article: article.id })}}
$ ./app/console cache_tags: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
This library is released under the MIT license. See the included LICENSE file for more information.