SlideShare a Scribd company logo
Cacheability metadata in Drupal 8
Joris Vercammen | @borisson
Cache metadata
Cache metadata
• Quick response
• Personalized pages
• Avoid unneeded cache clears
• Outputting things
• Cache system
• Advanced features
• Extra cache modules
• Outputting things
• Cache system
• Advanced features
• Extra cache modules
Outputting things
• Responses
• Renderable arrays
Outputting things
• Responses
• Renderable arrays
Responses
• Symfony components
• Http Foundation
use SymfonyComponentHttpFoundationResponse;
class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s');

return new Response("<p>Hello world, the time is: {$date} </p>");

}



}

Cache metadata
use DrupalCoreCacheCacheableResponse;
class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s');

return new CacheableResponse("<p>Hello world, the time is: {$date} </p>");

}



}

Cache metadata
Outputting things
• Responses
• Renderable arrays
class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s');

return ['#markup' => "<p>Hello world, the time is: {$date} </p>"];

}



}
Cache metadata
• Outputting things
• Cache system
• Advanced features
• Extra cache modules
Cache metadata
Cache system
• max-age
• tags
• context
• advanced
Cache system
• max-age
• tags
• context
• advanced
class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s');

return [

'#markup' => "<p>Hello world, the time is: {$date} </p>",

'#cache' => [

'max-age' => Cache::PERMANENT,

],

];

}



}
Cache metadata
class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s');

return [

'#markup' => "<p>Hello world, the time is: {$date} </p>",

'#cache' => [

'max-age' => 3,

],

];

}



}
Cache metadata
Cache system
• When does the representation of the thing I'm rendering become
outdated?
• (Cache::PERMANENT, 5, 60*60*24)
• ~ HTTP's Cache-Control: max-age header
Cache system
• max-age
• tags
• context
• advanced


class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s');

$title = (Node::load(1))->getTitle();

return [

'#markup' => "<p>Hello world, the time is: {$date}, node 1 title: {$title}</p>",

];

}



}

Cache metadata


class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s');

$title = (Node::load(1))->getTitle();

return [

'#markup' => "<p>Hello world, the time is: {$date}, node 1 title: {$title}</p>",

'#cache' => [

'tags' => ['node:1'],

],

];

}



}
Cache metadata
• What causes the representation of the thing I'm rendering become
outdated?
• entities (node:5, taxonomy_term:23,
search_api_index:database_index)
Cache system
• What causes the representation of the thing I'm rendering become
outdated?
• entities (node:5, taxonomy_term:23,
search_api_index:database_index)
Cache system
• max-age
• tags
• context
• advanced


class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s'); 

$arg = Drupal::request()->get('test');

return [

'#markup' => "<p>Hello world, the time is: {$date}, arg: {$arg}</p>",

];
}

}
Cache metadata


class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable("now"))->format('H:i:s'); 

$arg = Drupal::request()->get('test');

return [

'#markup' => "<p>Hello world, the time is: {$date}, arg: {$arg}</p>",

'#cache' => [

'contexts' => ['url.query_args'],

],

];
}

}
Cache metadata
Cache system
• Does the representation of the thing I'm rendering vary by
permissions, by URL, by interface language, by … something?
• (url, user.permissions, pirate_day)
• ~ HTTP's Vary header
Cache system
• max-age
• tags
• context
• advanced
Cache system
• Cache tags (data dependencies)
• Cache contexts (context dependencies)
• Cache max-age (time dependencies)
Cache system
• max-age
• tags
• context
• advanced


class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable(“now"))->format('H:i:s');
$node = Node::load(1);

return [

'#markup' => "<p>Hello world, the time is: {$date}</p>",

'#cache' => [

'max-age' => $node->getCacheMaxAge(),

'tags' => Cache::mergeTags(['test', $node->getCacheTags()]),

'contexts' => $node->getCacheContexts(),

],

];
}

}


class DefaultController extends ControllerBase {



public function hello() {

$date = (new DateTimeImmutable(“now"))->format('H:i:s');
$node = Node::load(1);


$renderer = Drupal::service('renderer');

$renderer->addCacheableDependency($node);


return [

'#markup' => "<p>Hello world, the time is: {$date}</p>”,
];
}

}


class DefaultController extends ControllerBase {



public function hello() { 

$date = (new DateTimeImmutable("now"))->format('H:i:s');



$build = [

'#markup' => "<p>Hello world, the time is: {$date}</p>",

];



$cache_data = new CacheableMetadata();

$cache_data->setCacheMaxAge(20);

$cache_data->applyTo($build);



return $build;
}

}
• Outputting things
• Cache system
• Extra cache modules
Bigpipe
Cache metadata
/**

* Class TestBlock

*

* @Block(

* id = "custom_block",

* admin_label = @Translation("Custom block")

* )

*/

class TestBlock extends BlockBase {



/**

* {@inheritdoc}

*/

public function build() {

$date = (new DateTimeImmutable("now"))->format('H:i:s');

sleep(4);



return [

'#markup' => "<p>The time is: {$date}</p>",

'#cache' => ['max-age' => 0],

];

}

}
Cache metadata
Bigpipe
• Renderable in isolation + poor cacheability
• autoplaceholder


renderer.config:

auto_placeholder_conditions:

max-age: 0

contexts:

- session

- user

tags: { }
Cache metadata
Cache metadata
Bigpipe
• Manual placeholders
services:

custom_test_module.service:

class: Drupalcustom_test_moduleTestService





namespace Drupalcustom_test_module;



class TestService {



public function callback($test) {
sleep(4);


return [

'#markup' => $date = (new DateTimeImmutable("now"))->format($test),

'#cache' => ['max-age' => 0],

];

}



}
/**

* Class TestBlock

*

* @Block(

* id = "custom_block",

* admin_label = @Translation("Custom block")

* )

*/

class TestBlock extends BlockBase {



public function build() {

return [

'#lazy_builder' => [‘custom_test_module.service:callback’, ['H:i:s']],

];

}


}
class TestBlock extends BlockBase {

public function build() {

$p = crc32('placeholder');

$format = 'H:i:s';

$time = (new DateTimeImmutable("now"))->format($format);

return [

'#markup' => 'The time is: ' . $time . ' | lazy time is: ' . $p,

'#attached' => [

'placeholders' => [

$p => [

'#lazy_builder' => [‘custom_test_module.service:callback’, [$format]],

'#cache' => [‘max-age' => ['0']],

],

],

],

];

}
}
Cache metadata
Refreshless
Cache metadata
Recap
page cache
dynamic page cache
bigpipe
refreshless
Questions?
More?
• https://drive.google.com/file/d/0By-njQ7nKRX-VndLdVozYUZac0E/view - @marcvangend
• http://wimleers.com/talk/making-drupal-fly-fastest-drupal-ever-here 

- @WimLeers
• http://wimleers.com/talk/bigpipe - @WimLeers
• http://wimleers.com/talk/drupal-8-render-pipeline @WimLeers

More Related Content

PPTX
MongoDB Aggregation
PDF
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
PDF
MongoDB Aggregation Framework
PPTX
Aggregation Framework
PPTX
Aggregation in MongoDB
PDF
Source Code for Dpilot
PDF
Dpilot Source Code With ScreenShots
PDF
XQuery in the Cloud
MongoDB Aggregation
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
MongoDB Aggregation Framework
Aggregation Framework
Aggregation in MongoDB
Source Code for Dpilot
Dpilot Source Code With ScreenShots
XQuery in the Cloud

What's hot (20)

PDF
Not your Grandma's XQuery
KEY
MongoDB Aggregation Framework
PDF
Aggregation Framework MongoDB Days Munich
ZIP
AnyMQ, Hippie, and the real-time web
PDF
Mongodb Aggregation Pipeline
PDF
PuppetCamp SEA @ Blk 71 - Nagios in under 10 mins with Puppet
PDF
PuppetCamp SEA @ Blk 71 - Nagios in under 10 mins with Puppet
PDF
Cutting Edge Data Processing with PHP & XQuery
PPTX
Webinar: Building Your First App in Node.js
PDF
FleetDB: A Schema-Free Database in Clojure
PDF
MongoDB .local Paris 2020: La puissance du Pipeline d'Agrégation de MongoDB
PDF
FleetDB A Schema-Free Database in Clojure
PDF
All Things Open 2016 -- Database Programming for Newbies
PDF
Monitoring Your ISP Using InfluxDB Cloud and Raspberry Pi
ODP
High Performance XQuery Processing in PHP with Zorba by Vikram Vaswani
KEY
DashProfiler 200807
PPT
Spring data iii
PPTX
Powershell for Log Analysis and Data Crunching
PPT
Camel one v3-6
PDF
Trading with opensource tools, two years later
Not your Grandma's XQuery
MongoDB Aggregation Framework
Aggregation Framework MongoDB Days Munich
AnyMQ, Hippie, and the real-time web
Mongodb Aggregation Pipeline
PuppetCamp SEA @ Blk 71 - Nagios in under 10 mins with Puppet
PuppetCamp SEA @ Blk 71 - Nagios in under 10 mins with Puppet
Cutting Edge Data Processing with PHP & XQuery
Webinar: Building Your First App in Node.js
FleetDB: A Schema-Free Database in Clojure
MongoDB .local Paris 2020: La puissance du Pipeline d'Agrégation de MongoDB
FleetDB A Schema-Free Database in Clojure
All Things Open 2016 -- Database Programming for Newbies
Monitoring Your ISP Using InfluxDB Cloud and Raspberry Pi
High Performance XQuery Processing in PHP with Zorba by Vikram Vaswani
DashProfiler 200807
Spring data iii
Powershell for Log Analysis and Data Crunching
Camel one v3-6
Trading with opensource tools, two years later
Ad

Similar to Cache metadata (20)

PPTX
Caching in drupal
PPTX
Render Caching for Drupal 8
PDF
Drupal 8 Render Cache
PDF
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
PDF
Drupal 8 Cache: Under the hood
ODP
Cache and Drupal
PPTX
Caching in Drupal 8
PDF
PhpTour Lyon 2014 - Transparent caching & context aware http cache
PDF
Joomla 1.6. caching implemented #jab11
PDF
Drupal Render API
PPT
Drupal Performance - SerBenfiquista.com Case Study
PDF
Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...
PPT
Drupal caching
PDF
Zend_Cache: how to improve the performance of PHP applications
PPT
eZ Publish Cluster Unleashed
PPTX
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
PPTX
Migrate yourself. code -> module -> mind
PPT
eZ Publish cluster unleashed revisited
PDF
Caching the Uncacheable
PDF
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Caching in drupal
Render Caching for Drupal 8
Drupal 8 Render Cache
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Drupal 8 Cache: Under the hood
Cache and Drupal
Caching in Drupal 8
PhpTour Lyon 2014 - Transparent caching & context aware http cache
Joomla 1.6. caching implemented #jab11
Drupal Render API
Drupal Performance - SerBenfiquista.com Case Study
Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...
Drupal caching
Zend_Cache: how to improve the performance of PHP applications
eZ Publish Cluster Unleashed
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Migrate yourself. code -> module -> mind
eZ Publish cluster unleashed revisited
Caching the Uncacheable
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Ad

Recently uploaded (20)

DOC
Rose毕业证学历认证,利物浦约翰摩尔斯大学毕业证国外本科毕业证
PDF
Slides PDF: The World Game (s) Eco Economic Epochs.pdf
PPTX
Mathew Digital SEO Checklist Guidlines 2025
PPT
12 Things That Make People Trust a Website Instantly
PPTX
The-Importance-of-School-Sanitation.pptx
PDF
The Evolution of Traditional to New Media .pdf
PPTX
Internet Safety for Seniors presentation
PPT
Ethics in Information System - Management Information System
PDF
The Ikigai Template _ Recalibrate How You Spend Your Time.pdf
PPTX
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
PDF
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
PPTX
E -tech empowerment technologies PowerPoint
PPTX
Slides PPTX: World Game (s): Eco Economic Epochs.pptx
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PDF
Session 1 (Week 1)fghjmgfdsfgthyjkhfdsadfghjkhgfdsa
PPTX
artificialintelligenceai1-copy-210604123353.pptx
PDF
Alethe Consulting Corporate Profile and Solution Aproach
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PDF
Smart Home Technology for Health Monitoring (www.kiu.ac.ug)
PPTX
Cyber Hygine IN organizations in MSME or
Rose毕业证学历认证,利物浦约翰摩尔斯大学毕业证国外本科毕业证
Slides PDF: The World Game (s) Eco Economic Epochs.pdf
Mathew Digital SEO Checklist Guidlines 2025
12 Things That Make People Trust a Website Instantly
The-Importance-of-School-Sanitation.pptx
The Evolution of Traditional to New Media .pdf
Internet Safety for Seniors presentation
Ethics in Information System - Management Information System
The Ikigai Template _ Recalibrate How You Spend Your Time.pdf
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
E -tech empowerment technologies PowerPoint
Slides PPTX: World Game (s): Eco Economic Epochs.pptx
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
Session 1 (Week 1)fghjmgfdsfgthyjkhfdsadfghjkhgfdsa
artificialintelligenceai1-copy-210604123353.pptx
Alethe Consulting Corporate Profile and Solution Aproach
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Smart Home Technology for Health Monitoring (www.kiu.ac.ug)
Cyber Hygine IN organizations in MSME or

Cache metadata