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";
}
}
The CacheTagsBundle library is available on Packagist. 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": "2.0.x-dev"
}
}
- Run the composer to download the bundle
$ composer require 'lbarulski/cache-tags-bundle:2.0.x-dev'
// 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, 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
Allows to spoof request host header; Optional, defaults to host
value
// 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;
}
{{ 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.