CacheTagsBundle
by Marcin Chyłek
Edge Side Includes (ESI)
BackendVarnish
WWW
article 1
article 2
article 3
article 4
Request content
Response content
Request/Response
content
ESI + CacheTagsBundle
BackendVarnish
WWW
article 1 (tag: tag_1)
article 2 (tag: tag_2)
article 3 (tag: tag_3)
article 4 (tag: tag_4)
Request content
Response content
Request/Response
content
What is CacheTagsBundle?
What is CacheTagsBundle?
● Tagging Response
What is CacheTagsBundle?
● Tagging Response
● Simple annotation configuration
What is CacheTagsBundle?
● Tagging Response
● Simple annotation configuration
● Cache Invalidation
How to Use CacheTagsBundle
● Plain cache tag
How to Use CacheTagsBundle
● Plain cache tag
● Request attribute tag
How to Use CacheTagsBundle
● Plain cache tag
Add annotation @CacheTagPlain to controller
use lbarulskiCacheTagsBundleAnnotationCacheTagPlain;
/**
* @CacheTagPlain("article_name")
**/
public function articleAction(Request $request)
{
}
How to Use CacheTagsBundle
● Plain cache tag
Prepare response
$response = new Response('...');
$response->setPublic();
$response->setTtl(3600);
How to Use CacheTagsBundle
● Request attribute tag
Implements TagInterface in model
use lbarulskiCacheTagsBundleTagTagInterface;
class Article implements TagInterface
{
public function getTag()
{
return 'article_'.$this->getId();
}
}
How to Use CacheTagsBundle
● Plain cache tag
Add annotation @CacheTagRequestAttribute to controller
use lbarulskiCacheTagsBundleAnnotationCacheTagRequestAttribute;
/**
* @ParamConverter("article")
* @CacheTagRequestAttribute("article")
**/
public function articleAction(Article $article)
{
}
How to Use CacheTagsBundle
● View Template: use esi controller
{{ render_esi(controller('MainBundle:Article:article', { article:
article.id })}}
How to Invalidate CacheTagsBundle
● Invalidate cache tags console
$ ./app/console cache_tags:invalidate tag-name
How to Invalidate CacheTagsBundle
● Invalidate cache tags in controller
use lbarulskiCacheTagsBundleTagPlain;
public function updateArticleAction(Article $article)
{
$tag = 'article_name';
$this->get('cache_tags.invalidator')->invalidate(new Plain($tag));
}
Link
● https://github.com/lbarulski/CacheTagsBundle
● https://www.varnish-cache.org/
marcin@chylek.pl
THANK YOU

CacheTagsBundle

  • 1.
  • 2.
    Edge Side Includes(ESI) BackendVarnish WWW article 1 article 2 article 3 article 4 Request content Response content Request/Response content
  • 3.
    ESI + CacheTagsBundle BackendVarnish WWW article1 (tag: tag_1) article 2 (tag: tag_2) article 3 (tag: tag_3) article 4 (tag: tag_4) Request content Response content Request/Response content
  • 4.
  • 5.
  • 6.
    What is CacheTagsBundle? ●Tagging Response ● Simple annotation configuration
  • 7.
    What is CacheTagsBundle? ●Tagging Response ● Simple annotation configuration ● Cache Invalidation
  • 8.
    How to UseCacheTagsBundle ● Plain cache tag
  • 9.
    How to UseCacheTagsBundle ● Plain cache tag ● Request attribute tag
  • 10.
    How to UseCacheTagsBundle ● Plain cache tag Add annotation @CacheTagPlain to controller use lbarulskiCacheTagsBundleAnnotationCacheTagPlain; /** * @CacheTagPlain("article_name") **/ public function articleAction(Request $request) { }
  • 11.
    How to UseCacheTagsBundle ● Plain cache tag Prepare response $response = new Response('...'); $response->setPublic(); $response->setTtl(3600);
  • 12.
    How to UseCacheTagsBundle ● Request attribute tag Implements TagInterface in model use lbarulskiCacheTagsBundleTagTagInterface; class Article implements TagInterface { public function getTag() { return 'article_'.$this->getId(); } }
  • 13.
    How to UseCacheTagsBundle ● Plain cache tag Add annotation @CacheTagRequestAttribute to controller use lbarulskiCacheTagsBundleAnnotationCacheTagRequestAttribute; /** * @ParamConverter("article") * @CacheTagRequestAttribute("article") **/ public function articleAction(Article $article) { }
  • 14.
    How to UseCacheTagsBundle ● View Template: use esi controller {{ render_esi(controller('MainBundle:Article:article', { article: article.id })}}
  • 15.
    How to InvalidateCacheTagsBundle ● Invalidate cache tags console $ ./app/console cache_tags:invalidate tag-name
  • 16.
    How to InvalidateCacheTagsBundle ● Invalidate cache tags in controller use lbarulskiCacheTagsBundleTagPlain; public function updateArticleAction(Article $article) { $tag = 'article_name'; $this->get('cache_tags.invalidator')->invalidate(new Plain($tag)); }
  • 17.
  • 18.