SlideShare a Scribd company logo
Render Caching in Drupal 8
by. John Doyle
10/2/2017
1/26/17
Who are we?
Full service provider of
branding, marketing, website
design & development, and
strategic communication
services
Proven methodology for
positioning companies to
scale and succeed in an
increasingly digital
environment
Trusted agency partner to
dozens of recognized
associations and
corporations
Fortune 500
Awards
My Background
・Credentials: B.S. Computer Science from
Virginia Tech
・Position: Chief Technology Officer @
Bluetext
・Interests: IoT, Gaming, Snowboarding,
Hiking, Fishing, etc…
Why are we here?
Why are we here?
・Render Arrays…
・Recap of Render Caching in D7
・Drupal 8 Render Caching
・Random Useful Things
10
Drupal 7 Render Arrays
Render Caching in Drupal 7
・Render Arrays provide a #cache property
・Pre-render property is used to handle complex logic before it
gets cached in the render array
13
Example Render Array in Drupal 7
function render_example_arrays() {
$interval = 60;
$demo['cache demonstration'] = array(
'#markup' => '',
'#pre_render' => array('render_example_cache_pre_render'),
'#cache' => array(
'keys' => array('render_example', 'cache', 'demonstration'),
'bin' => 'cache',
'expire' => time() + $interval,
'granularity' => DRUPAL_CACHE_PER_PAGE |
DRUPAL_CACHE_PER_ROLE,
),
);
}
14
・ 'keys' => an array of keys which will be
concatenated to form the cache key.
・ 'bin' => the name of the cache bin to be
used (as in 'cache' or 'cache_page', etc.
・ 'expire' => a Unix timestamp indicating
the expiration time of the cache.
・ 'granularity' => a bitmask indicating the
cache type. This should
be DRUPAL_CACHE_PER_PAGE, DRUPA
L_CACHE_PER_ROLE,
or DRUPAL_CACHE_PER_USER
Drupal 7 Render Cache Module
・#pre_render is used for
expensive callback functions
・If the logic was executed
in #markup or in the
render_example_arrays
( ) function, it would
never be cached.
・#cache is used to define
cache settings
15
function render_example_arrays() {
$interval = 60;
$demo['cache demonstration'] = array(
'#markup' => '',
'#pre_render' => array('render_example_cache_pre_render'),
'#cache' => array(
'keys' => array('render_example', 'cache', 'demonstration'),
'bin' => 'cache',
'expire' => time() + $interval,
'granularity' => DRUPAL_CACHE_PER_PAGE |
DRUPAL_CACHE_PER_ROLE,
),
);
}
Clearing the Cache in Drupal 7
16
//Clear a specific Cached Entity
cache_clear_all('cache:group:id', $bin);
//Clear a specific Cached Entity
cache_clear_all('cache:group', $bin, TRUE);
//Clear a specific Cached Entity
cache_clear_all('*', $bin, TRUE);
So… Whats the problem?
・Clearing cached pages that reference the entity I am clearing
・Referenced Entities
・Parent Entities
・Entities in Views
・etc…
17
Further Reading
・For more on Drupal 7 Render Caching:
・Render Arrays in Drupal 7
・Render Cache Module
Drupal 8 - The New World
Drupal 8 Render Array #cache property
22
• 'keys': Identifiers for cacheable portions of render arrays. These should be created and added
for portions of a render array that involve expensive calculations in the rendering process.
• 'contexts': Contexts that may affect rendering, such as user role and language. When no
context is specified, it means that the render array does not vary by any context.
• 'tags': Tags for data that rendering depends on, such as for individual nodes or user accounts,
so that when these change the cache can be automatically invalidated. If the data consists of
entities, you can use DrupalCoreEntityEntityInterface::getCacheTags() to generate
appropriate tags; configuration objects have a similar method.
• 'max-age': The maximum duration for which a render array may be cached. Defaults
to DrupalCoreCacheCache::PERMANENT (permanently cacheable).
'#cache' => [
'keys' => ['entity_view', 'node', $node->id()],
'contexts' => ['languages'],
'tags' => ['node:' . $node->id()],
'max-age' => Cache::PERMANENT,
],
Cache Keys
・Cache Keys are a representation of a set of code that you want
to make cacheable
・Typically something that is too expensive to render on
every page load
・Example: ‘entity_view’
23
Cache Context
・Cache Contexts define variations of what I am rendering.
・Does my display change based on user role?
・Does my display change based on language?
・Does my display change based on time of day?
・Does my display change based on location?
・Does my display change based on <insert here>?
24
Cache Tags
・Cache Tags outline identifiers that this render array depends
on to render properly
・Referenced Entity ID’s (Nodes, Paragraphs, Users, etc…)
・Custom Identifiers
25
Cache max-age
・Cache max-age determines how long the item I am rendering
can be cached for
・Defaults to “Permanent”
・Must always be set
26
Cache Tags
・Cache Tags are where the magic happens
・Cache Tags allow you to:
・identify dependencies to a render object
・invalidate pages where a child node is referenced across
the entire site
27
Cache Tags
・Cache Tags are where the headache happens
・Cache Tags force you to:
・Think about how you are rendering elements
・Plan out your caching architecture
・Do some real debugging in the theme layer when things
aren’t working
・Bang your head on your keyboard trying to figure out why
there are issues
28
Bubbling Your Cache Tags
・Getting your cache tags to bubble up correctly can be a
challenge
・Must render the #cache property of the render array in
twig templates
・Must handle bubbling cache tags manually if you manually
render entity displays
30
Example Twig Outputs
31
What Works:
・Render Entire Content Array
・Render Content Array without
fields
・Render Child Content Cache
Array
・This example is for a viewfield
<div class="content">
{{ content }}
</div>
<div class="content">
{{ content|without('comment', 'links') }}
</div>
{{ attribute(content['field_dynamic_content']|render,
'#cache') }}
Failing to Render the Content Array
・Failing to render the #cache property of the {{ content }} array
will result in failing to bubble up the proper cache tags
・This will lead to lots of debugging and replacement keyboards
32
More Random Useful Things
Advanced Topics: Lazy Loading
・Auto-placeholdering is the process of detecting poorly
cacheable (highly dynamic) pieces of your page and rendering
them later in the render process
・Uses the #lazy_builder callback
・More information: Drupal 8 Auto Placeholdering
34
Lazy Loading – Block Definition
35
• Define a block and set the build to
a #lazy_builder callback
• #create_placeholder = TRUE is
required here
• Set the cache to 0 (no-cache)
/**
* Provides a 'Lazy Block' block.
*
* @Block(
* id = "lazy_block",
* admin_label = @Translation("Lazy Block")
* )
*/
class LazyBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$build['form'] = array(
'#lazy_builder' => array(
'demo_lazy_builder.form_load:loadLazyBlock',
array()
),
'#create_placeholder' => TRUE,
'#cache' => ['contexts' => [],
'max-age' => 0],
);
$build['#cache'] = ['max-age' => 0];
return $build;
}
}
Lazy Loading - Service
36
• Define your service callback
• Define your class that handles the
callback
services:
demo_lazy_builder.form_load:
class: Drupaldemo_lazy_builderLazyBlockLoader
arguments: []
Lazy Loading – Dynamic Logic
37
• Define a callback for your render
array with #lazy_builder
/**
* Class LazyBlockLoader.
*
* @package DrupalLazyBlockLoader
*/
class LazyBlockLoader {
/**
* @return array
*/
public function loadLazyBlock() {
$value = [
'#markup' => time(),
'#cache' => ['max-age' => 0],
];
return $value;
}
}
Cache Tag: node_list
・The node_list cache tag will get triggered to invalidate when
any node CRUD operation
・Useful for listing pages when you want new nodes to appear
・Want better performance?
・ View Custom Tags Module
Purge
・Purge is in a state where it is usable on production websites
・Acquia Purge has gone into Public Beta
・More information:
・Purge Module
・Acquia Purge Q&A
39
Known Issues/Troubleshooting
How can I Troubleshoot?
・Step 1: Open your twig template and render the {{content}}
array and see if it magically starts working.
・Step 2: Check your Manage Display options to ensure the
proper display formatter is selected. (ie. Rendered Entity)
・Step 3: Check out the cache tables to see what tags are
bubbling up.
・Step 4: Search the interwebs for known issues.
・Step 5: Patch or write some custom code to resolve the issue.
What do render cache tags look like?
・Cache Tags are a list of entity:id
・There are multiple render cache entries for each entity
・Role Based (Anonymous, Authenticated, etc…)
・Entity View Mode (Default, Teaser, Search Result, etc...)
node:1016 node:57541 node:57546 node:57551 node_view rendered user:1
config:filter.format.rich_text node:1016 node:57541 node:57546 node:57551 node_view rendered user:1
Confused Render Templates
・Scenario
・Internal Cache + Internal Dynamic Cache
・View Displays
・Node templates using node--view--<view_name>.html.twig
・Result
・View templates rendering the wrong node--view templates
・More info: https://www.drupal.org/node/2838950
Manage Display Matters
・Proper configuration of the Manage Display options matter.
・Example:
・Rendering a paragraph using Entity Label instead of
Rendered Entity will not bubble cache tags
・Example Issue w/ Paragraphs not bubbling up cache tags (It
does work when configured properly):
https://www.drupal.org/node/2855735
Questions?
Check us out:
www.bluetext.com
Drupal 8 References
・Cachability of Render Arrays
・Render API Overview
47

More Related Content

What's hot

Symfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODMSymfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODM
Jonathan Wage
 
What's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field APIWhat's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field API
Drupalize.Me
 
Building performance auf der Developer Conference Hamburg
Building performance auf der Developer Conference HamburgBuilding performance auf der Developer Conference Hamburg
Building performance auf der Developer Conference Hamburg
Oliver Ochs
 
Drupal as a web framework
Drupal as a web frameworkDrupal as a web framework
Drupal as a web framework
Adam Kalsey
 
Field api.From d7 to d8
Field api.From d7 to d8Field api.From d7 to d8
Field api.From d7 to d8
Pavel Makhrinsky
 
Intro to IndexedDB (Beta)
Intro to IndexedDB (Beta)Intro to IndexedDB (Beta)
Intro to IndexedDB (Beta)
Mike West
 
Please Don't Touch the Slow Parts V3
Please Don't Touch the Slow Parts V3Please Don't Touch the Slow Parts V3
Please Don't Touch the Slow Parts V3
Federico Galassi
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usage
Pavel Makhrinsky
 
How browser engines work?
How browser engines work?How browser engines work?
How browser engines work?
haricot
 
Please dont touch-3.5
Please dont touch-3.5Please dont touch-3.5
Please dont touch-3.5
Francesco Fullone
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
Pavel Makhrinsky
 
Spring Data MongoDB 介紹
Spring Data MongoDB 介紹Spring Data MongoDB 介紹
Spring Data MongoDB 介紹
Kuo-Chun Su
 
Migrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Migrating to Drupal 8: How to Migrate Your Content and Minimize the RisksMigrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Migrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Acquia
 
Drupal Render API
Drupal Render APIDrupal Render API
Drupal Render API
Pavel Makhrinsky
 
Varnish Cache and its usage in the real world!
Varnish Cache and its usage in the real world!Varnish Cache and its usage in the real world!
Varnish Cache and its usage in the real world!
Ivan Chepurnyi
 
Kharkivpy#3: Javascript and Python backend
Kharkivpy#3: Javascript and Python backendKharkivpy#3: Javascript and Python backend
Kharkivpy#3: Javascript and Python backend
Max Klymyshyn
 
WooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda Bagus
WordCamp Indonesia
 
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql  - how do pdo, mysq-li, and x devapi do what they doPhp &amp; my sql  - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Dave Stokes
 
MyBatis
MyBatisMyBatis
MyBatis
Roman Dovgan
 
Drupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodDrupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hood
Piyuesh Kumar
 

What's hot (20)

Symfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODMSymfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODM
 
What's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field APIWhat's New in Drupal 8: Entity Field API
What's New in Drupal 8: Entity Field API
 
Building performance auf der Developer Conference Hamburg
Building performance auf der Developer Conference HamburgBuilding performance auf der Developer Conference Hamburg
Building performance auf der Developer Conference Hamburg
 
Drupal as a web framework
Drupal as a web frameworkDrupal as a web framework
Drupal as a web framework
 
Field api.From d7 to d8
Field api.From d7 to d8Field api.From d7 to d8
Field api.From d7 to d8
 
Intro to IndexedDB (Beta)
Intro to IndexedDB (Beta)Intro to IndexedDB (Beta)
Intro to IndexedDB (Beta)
 
Please Don't Touch the Slow Parts V3
Please Don't Touch the Slow Parts V3Please Don't Touch the Slow Parts V3
Please Don't Touch the Slow Parts V3
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usage
 
How browser engines work?
How browser engines work?How browser engines work?
How browser engines work?
 
Please dont touch-3.5
Please dont touch-3.5Please dont touch-3.5
Please dont touch-3.5
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
 
Spring Data MongoDB 介紹
Spring Data MongoDB 介紹Spring Data MongoDB 介紹
Spring Data MongoDB 介紹
 
Migrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Migrating to Drupal 8: How to Migrate Your Content and Minimize the RisksMigrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Migrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
 
Drupal Render API
Drupal Render APIDrupal Render API
Drupal Render API
 
Varnish Cache and its usage in the real world!
Varnish Cache and its usage in the real world!Varnish Cache and its usage in the real world!
Varnish Cache and its usage in the real world!
 
Kharkivpy#3: Javascript and Python backend
Kharkivpy#3: Javascript and Python backendKharkivpy#3: Javascript and Python backend
Kharkivpy#3: Javascript and Python backend
 
WooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda Bagus
 
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql  - how do pdo, mysq-li, and x devapi do what they doPhp &amp; my sql  - how do pdo, mysq-li, and x devapi do what they do
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
 
MyBatis
MyBatisMyBatis
MyBatis
 
Drupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodDrupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hood
 

Similar to Render Caching for Drupal 8

SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
Mark Rackley
 
Introduction to Client Side Dev in SharePoint Workshop
Introduction to Client Side Dev in SharePoint WorkshopIntroduction to Client Side Dev in SharePoint Workshop
Introduction to Client Side Dev in SharePoint Workshop
Mark Rackley
 
Does my DIV look big in this?
Does my DIV look big in this?Does my DIV look big in this?
Does my DIV look big in this?
glen_a_smith
 
Drupal 8 Render Cache
Drupal 8 Render CacheDrupal 8 Render Cache
Drupal 8 Render Cache
Erik Stielstra
 
Visualizing Content with Display Suite
Visualizing Content with Display SuiteVisualizing Content with Display Suite
Visualizing Content with Display Suite
Matthias Vandermaesen
 
To build a WordPress Theme: Wordcamp Denmark 2014
To build a WordPress Theme: Wordcamp Denmark 2014To build a WordPress Theme: Wordcamp Denmark 2014
To build a WordPress Theme: Wordcamp Denmark 2014
James Bonham
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
Alive Kuo
 
ONE MORE TIME ABOUT CODE STANDARDS AND BEST PRACTICES
ONE MORE TIME ABOUT CODE STANDARDS AND BEST PRACTICESONE MORE TIME ABOUT CODE STANDARDS AND BEST PRACTICES
ONE MORE TIME ABOUT CODE STANDARDS AND BEST PRACTICES
DrupalCamp Kyiv
 
Plain english guide to drupal 8 criticals
Plain english guide to drupal 8 criticalsPlain english guide to drupal 8 criticals
Plain english guide to drupal 8 criticals
Angela Byron
 
Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.
Nelson Gomes
 
Display Suite: A Themers Perspective
Display Suite: A Themers PerspectiveDisplay Suite: A Themers Perspective
Display Suite: A Themers Perspective
Mediacurrent
 
Tips for Building your First XPages Java Application
Tips for Building your First XPages Java ApplicationTips for Building your First XPages Java Application
Tips for Building your First XPages Java Application
Teamstudio
 
Using VueJS in front of Drupal 8
Using VueJS in front of Drupal 8Using VueJS in front of Drupal 8
Using VueJS in front of Drupal 8
Brian Ward
 
Ruby For Startups
Ruby For StartupsRuby For Startups
Ruby For Startups
Mike Subelsky
 
Building and Maintaining a Distribution in Drupal 7 with Features
Building and Maintaining a  Distribution in Drupal 7 with FeaturesBuilding and Maintaining a  Distribution in Drupal 7 with Features
Building and Maintaining a Distribution in Drupal 7 with Features
Nuvole
 
Scout xss csrf_security_presentation_chicago
Scout xss csrf_security_presentation_chicagoScout xss csrf_security_presentation_chicago
Scout xss csrf_security_presentation_chicago
knaddison
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalability
Twinbit
 
Yii Introduction
Yii IntroductionYii Introduction
Yii Introduction
Jason Ragsdale
 
Quality code by design
Quality code by designQuality code by design
Quality code by design
WP Developers Club
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
Dr Nic Williams
 

Similar to Render Caching for Drupal 8 (20)

SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
 
Introduction to Client Side Dev in SharePoint Workshop
Introduction to Client Side Dev in SharePoint WorkshopIntroduction to Client Side Dev in SharePoint Workshop
Introduction to Client Side Dev in SharePoint Workshop
 
Does my DIV look big in this?
Does my DIV look big in this?Does my DIV look big in this?
Does my DIV look big in this?
 
Drupal 8 Render Cache
Drupal 8 Render CacheDrupal 8 Render Cache
Drupal 8 Render Cache
 
Visualizing Content with Display Suite
Visualizing Content with Display SuiteVisualizing Content with Display Suite
Visualizing Content with Display Suite
 
To build a WordPress Theme: Wordcamp Denmark 2014
To build a WordPress Theme: Wordcamp Denmark 2014To build a WordPress Theme: Wordcamp Denmark 2014
To build a WordPress Theme: Wordcamp Denmark 2014
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
ONE MORE TIME ABOUT CODE STANDARDS AND BEST PRACTICES
ONE MORE TIME ABOUT CODE STANDARDS AND BEST PRACTICESONE MORE TIME ABOUT CODE STANDARDS AND BEST PRACTICES
ONE MORE TIME ABOUT CODE STANDARDS AND BEST PRACTICES
 
Plain english guide to drupal 8 criticals
Plain english guide to drupal 8 criticalsPlain english guide to drupal 8 criticals
Plain english guide to drupal 8 criticals
 
Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.
 
Display Suite: A Themers Perspective
Display Suite: A Themers PerspectiveDisplay Suite: A Themers Perspective
Display Suite: A Themers Perspective
 
Tips for Building your First XPages Java Application
Tips for Building your First XPages Java ApplicationTips for Building your First XPages Java Application
Tips for Building your First XPages Java Application
 
Using VueJS in front of Drupal 8
Using VueJS in front of Drupal 8Using VueJS in front of Drupal 8
Using VueJS in front of Drupal 8
 
Ruby For Startups
Ruby For StartupsRuby For Startups
Ruby For Startups
 
Building and Maintaining a Distribution in Drupal 7 with Features
Building and Maintaining a  Distribution in Drupal 7 with FeaturesBuilding and Maintaining a  Distribution in Drupal 7 with Features
Building and Maintaining a Distribution in Drupal 7 with Features
 
Scout xss csrf_security_presentation_chicago
Scout xss csrf_security_presentation_chicagoScout xss csrf_security_presentation_chicago
Scout xss csrf_security_presentation_chicago
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalability
 
Yii Introduction
Yii IntroductionYii Introduction
Yii Introduction
 
Quality code by design
Quality code by designQuality code by design
Quality code by design
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 

Recently uploaded

Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 

Recently uploaded (20)

Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 

Render Caching for Drupal 8

  • 1. Render Caching in Drupal 8 by. John Doyle 10/2/2017 1/26/17
  • 2. Who are we? Full service provider of branding, marketing, website design & development, and strategic communication services Proven methodology for positioning companies to scale and succeed in an increasingly digital environment Trusted agency partner to dozens of recognized associations and corporations
  • 4.
  • 5.
  • 6.
  • 8. My Background ・Credentials: B.S. Computer Science from Virginia Tech ・Position: Chief Technology Officer @ Bluetext ・Interests: IoT, Gaming, Snowboarding, Hiking, Fishing, etc…
  • 9. Why are we here?
  • 10. Why are we here? ・Render Arrays… ・Recap of Render Caching in D7 ・Drupal 8 Render Caching ・Random Useful Things 10
  • 11. Drupal 7 Render Arrays
  • 12.
  • 13. Render Caching in Drupal 7 ・Render Arrays provide a #cache property ・Pre-render property is used to handle complex logic before it gets cached in the render array 13
  • 14. Example Render Array in Drupal 7 function render_example_arrays() { $interval = 60; $demo['cache demonstration'] = array( '#markup' => '', '#pre_render' => array('render_example_cache_pre_render'), '#cache' => array( 'keys' => array('render_example', 'cache', 'demonstration'), 'bin' => 'cache', 'expire' => time() + $interval, 'granularity' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE, ), ); } 14 ・ 'keys' => an array of keys which will be concatenated to form the cache key. ・ 'bin' => the name of the cache bin to be used (as in 'cache' or 'cache_page', etc. ・ 'expire' => a Unix timestamp indicating the expiration time of the cache. ・ 'granularity' => a bitmask indicating the cache type. This should be DRUPAL_CACHE_PER_PAGE, DRUPA L_CACHE_PER_ROLE, or DRUPAL_CACHE_PER_USER
  • 15. Drupal 7 Render Cache Module ・#pre_render is used for expensive callback functions ・If the logic was executed in #markup or in the render_example_arrays ( ) function, it would never be cached. ・#cache is used to define cache settings 15 function render_example_arrays() { $interval = 60; $demo['cache demonstration'] = array( '#markup' => '', '#pre_render' => array('render_example_cache_pre_render'), '#cache' => array( 'keys' => array('render_example', 'cache', 'demonstration'), 'bin' => 'cache', 'expire' => time() + $interval, 'granularity' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE, ), ); }
  • 16. Clearing the Cache in Drupal 7 16 //Clear a specific Cached Entity cache_clear_all('cache:group:id', $bin); //Clear a specific Cached Entity cache_clear_all('cache:group', $bin, TRUE); //Clear a specific Cached Entity cache_clear_all('*', $bin, TRUE);
  • 17. So… Whats the problem? ・Clearing cached pages that reference the entity I am clearing ・Referenced Entities ・Parent Entities ・Entities in Views ・etc… 17
  • 18. Further Reading ・For more on Drupal 7 Render Caching: ・Render Arrays in Drupal 7 ・Render Cache Module
  • 19. Drupal 8 - The New World
  • 20.
  • 21.
  • 22. Drupal 8 Render Array #cache property 22 • 'keys': Identifiers for cacheable portions of render arrays. These should be created and added for portions of a render array that involve expensive calculations in the rendering process. • 'contexts': Contexts that may affect rendering, such as user role and language. When no context is specified, it means that the render array does not vary by any context. • 'tags': Tags for data that rendering depends on, such as for individual nodes or user accounts, so that when these change the cache can be automatically invalidated. If the data consists of entities, you can use DrupalCoreEntityEntityInterface::getCacheTags() to generate appropriate tags; configuration objects have a similar method. • 'max-age': The maximum duration for which a render array may be cached. Defaults to DrupalCoreCacheCache::PERMANENT (permanently cacheable). '#cache' => [ 'keys' => ['entity_view', 'node', $node->id()], 'contexts' => ['languages'], 'tags' => ['node:' . $node->id()], 'max-age' => Cache::PERMANENT, ],
  • 23. Cache Keys ・Cache Keys are a representation of a set of code that you want to make cacheable ・Typically something that is too expensive to render on every page load ・Example: ‘entity_view’ 23
  • 24. Cache Context ・Cache Contexts define variations of what I am rendering. ・Does my display change based on user role? ・Does my display change based on language? ・Does my display change based on time of day? ・Does my display change based on location? ・Does my display change based on <insert here>? 24
  • 25. Cache Tags ・Cache Tags outline identifiers that this render array depends on to render properly ・Referenced Entity ID’s (Nodes, Paragraphs, Users, etc…) ・Custom Identifiers 25
  • 26. Cache max-age ・Cache max-age determines how long the item I am rendering can be cached for ・Defaults to “Permanent” ・Must always be set 26
  • 27. Cache Tags ・Cache Tags are where the magic happens ・Cache Tags allow you to: ・identify dependencies to a render object ・invalidate pages where a child node is referenced across the entire site 27
  • 28. Cache Tags ・Cache Tags are where the headache happens ・Cache Tags force you to: ・Think about how you are rendering elements ・Plan out your caching architecture ・Do some real debugging in the theme layer when things aren’t working ・Bang your head on your keyboard trying to figure out why there are issues 28
  • 29.
  • 30. Bubbling Your Cache Tags ・Getting your cache tags to bubble up correctly can be a challenge ・Must render the #cache property of the render array in twig templates ・Must handle bubbling cache tags manually if you manually render entity displays 30
  • 31. Example Twig Outputs 31 What Works: ・Render Entire Content Array ・Render Content Array without fields ・Render Child Content Cache Array ・This example is for a viewfield <div class="content"> {{ content }} </div> <div class="content"> {{ content|without('comment', 'links') }} </div> {{ attribute(content['field_dynamic_content']|render, '#cache') }}
  • 32. Failing to Render the Content Array ・Failing to render the #cache property of the {{ content }} array will result in failing to bubble up the proper cache tags ・This will lead to lots of debugging and replacement keyboards 32
  • 34. Advanced Topics: Lazy Loading ・Auto-placeholdering is the process of detecting poorly cacheable (highly dynamic) pieces of your page and rendering them later in the render process ・Uses the #lazy_builder callback ・More information: Drupal 8 Auto Placeholdering 34
  • 35. Lazy Loading – Block Definition 35 • Define a block and set the build to a #lazy_builder callback • #create_placeholder = TRUE is required here • Set the cache to 0 (no-cache) /** * Provides a 'Lazy Block' block. * * @Block( * id = "lazy_block", * admin_label = @Translation("Lazy Block") * ) */ class LazyBlock extends BlockBase { /** * {@inheritdoc} */ public function build() { $build['form'] = array( '#lazy_builder' => array( 'demo_lazy_builder.form_load:loadLazyBlock', array() ), '#create_placeholder' => TRUE, '#cache' => ['contexts' => [], 'max-age' => 0], ); $build['#cache'] = ['max-age' => 0]; return $build; } }
  • 36. Lazy Loading - Service 36 • Define your service callback • Define your class that handles the callback services: demo_lazy_builder.form_load: class: Drupaldemo_lazy_builderLazyBlockLoader arguments: []
  • 37. Lazy Loading – Dynamic Logic 37 • Define a callback for your render array with #lazy_builder /** * Class LazyBlockLoader. * * @package DrupalLazyBlockLoader */ class LazyBlockLoader { /** * @return array */ public function loadLazyBlock() { $value = [ '#markup' => time(), '#cache' => ['max-age' => 0], ]; return $value; } }
  • 38. Cache Tag: node_list ・The node_list cache tag will get triggered to invalidate when any node CRUD operation ・Useful for listing pages when you want new nodes to appear ・Want better performance? ・ View Custom Tags Module
  • 39. Purge ・Purge is in a state where it is usable on production websites ・Acquia Purge has gone into Public Beta ・More information: ・Purge Module ・Acquia Purge Q&A 39
  • 41. How can I Troubleshoot? ・Step 1: Open your twig template and render the {{content}} array and see if it magically starts working. ・Step 2: Check your Manage Display options to ensure the proper display formatter is selected. (ie. Rendered Entity) ・Step 3: Check out the cache tables to see what tags are bubbling up. ・Step 4: Search the interwebs for known issues. ・Step 5: Patch or write some custom code to resolve the issue.
  • 42. What do render cache tags look like? ・Cache Tags are a list of entity:id ・There are multiple render cache entries for each entity ・Role Based (Anonymous, Authenticated, etc…) ・Entity View Mode (Default, Teaser, Search Result, etc...) node:1016 node:57541 node:57546 node:57551 node_view rendered user:1 config:filter.format.rich_text node:1016 node:57541 node:57546 node:57551 node_view rendered user:1
  • 43. Confused Render Templates ・Scenario ・Internal Cache + Internal Dynamic Cache ・View Displays ・Node templates using node--view--<view_name>.html.twig ・Result ・View templates rendering the wrong node--view templates ・More info: https://www.drupal.org/node/2838950
  • 44. Manage Display Matters ・Proper configuration of the Manage Display options matter. ・Example: ・Rendering a paragraph using Entity Label instead of Rendered Entity will not bubble cache tags ・Example Issue w/ Paragraphs not bubbling up cache tags (It does work when configured properly): https://www.drupal.org/node/2855735
  • 47. Drupal 8 References ・Cachability of Render Arrays ・Render API Overview 47

Editor's Notes

  1. Example: Current view is rendering rendered entity using “Search Result” Expected Result: Content Item displayed using search result template. Actual Result: Content item displayed using the node—view—viewtemplate.