lbarulski / cache-tags-bundle
Bundle for custom varnish tags
Installs: 120 176
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 4
Open Issues: 0
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 is simple Cache Tag and Cache Invalidation for 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";
}
}
Installation
The CacheTagsBundle library is available on Packagist. You can install it using [Composer] (http://getcomposer.org):
Method 1
Simply run assuming you have installed composer.phar or composer binary:
$ composer require lbarulski/cache-tags-bundle
Method 2
- 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'
Add this bundle to your application's kernel
// app/ApplicationKernel.php public function registerBundles() { return array( // ... new lbarulski\CacheTagsBundle\CacheTagsBundle(), // ... ); }
Configuration config.yml example
# 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
Usage examples:
Controller: plain 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; }
Controller: request attribute tag
// 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; }
View Template: use esi controller
{{ render_esi(controller('MainBundle:Article:article', { article: article.id })}}
Invalidate
Command 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)); ... }
Presentation
http://www.slideshare.net/chylek/cachetagsbundle
License
This library is released under the MIT license. See the included LICENSE file for more information.