SlideShare ist ein Scribd-Unternehmen logo
1 von 47
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

Weitere ähnliche Inhalte

Was ist angesagt?

Symfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODMSymfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODMJonathan 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 APIDrupalize.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 HamburgOliver Ochs
 
Drupal as a web framework
Drupal as a web frameworkDrupal as a web framework
Drupal as a web frameworkAdam Kalsey
 
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 V3Federico Galassi
 
Drupal Field API. Practical usage
Drupal Field API. Practical usageDrupal Field API. Practical usage
Drupal Field API. Practical usagePavel Makhrinsky
 
How browser engines work?
How browser engines work?How browser engines work?
How browser engines work?haricot
 
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 RisksAcquia
 
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 backendMax 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 BagusWordCamp 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 doDave Stokes
 
Drupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodDrupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodPiyuesh Kumar
 

Was ist angesagt? (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
 

Ähnlich wie Render Caching for Drupal 8

SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsMark 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 WorkshopMark 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
 
Visualizing Content with Display Suite
Visualizing Content with Display SuiteVisualizing Content with Display Suite
Visualizing Content with Display SuiteMatthias 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 2014James Bonham
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive 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 PRACTICESDrupalCamp 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 criticalsAngela 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 PerspectiveMediacurrent
 
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 ApplicationTeamstudio
 
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 8Brian Ward
 
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 FeaturesNuvole
 
Scout xss csrf_security_presentation_chicago
Scout xss csrf_security_presentation_chicagoScout xss csrf_security_presentation_chicago
Scout xss csrf_security_presentation_chicagoknaddison
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalabilityTwinbit
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteDr Nic Williams
 

Ähnlich wie 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
 

Kürzlich hochgeladen

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

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

Hinweis der Redaktion

  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.