SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
2
Cache it!
Render cache in Drupal 8
3
About Wizzlern
Wizzlern
‣ 100% Drupal

‣ Drupal training and consultancy

‣ Front-, backend-development 

‣ Site administration and editorial

‣ Advanced subjects

‣ Since 2009

‣ Active in the Drupal community

‣ Visit us at http://wizzlern.nl
4
5
Program
Drupal 8 Render Cache
‣ D7-D8 differences

‣ Cache terminology

‣ The thought process

‣ Demonstration:

‣ Recent content list

‣ Dynamic advertisement
6
Caching
7
“There	
  are	
  only	
  two	
  hard	
  
things	
  in	
  Computer	
  Science:	
  

cache	
  invalidation	
  and	
  
naming	
  things.”
Phil Karlton
8
Drupal 7 - Page cache
‣ Core: Cache at page level.

‣ Low complexity cache invalidation.

‣ When a page is edited, the whole page cache
is cleared.

‣ When a comment is added, the whole page
cache is cleared.
9
Drupal 7 - Contrib cache
‣ Cache Expiration and various Varnish
modules: High complexity cache invalidation:

‣ purge / expire all URLs that contains the changing
content or is referring to it.

‣ High invalidation costs.
10
Drupal 8 Render Cache
11
Drupal 8 strategy
‣ High-complexity cache invalidation

‣ Content invalidated instantaneously

‣ Cached permanently

‣ Cache chaining

‣ Rendered data cached at multiple levels:

‣ Page, Block, Entity (node, comment, user, etc.),
View, Menu tree, Text Format filter result, etc.
12
Render #cache
13
$build['#cache'] = array(

'tags' => ['block_view', 'config:block.block.recentcontent'],

'contexts' => ['languages:language_interface', 'user.permissions'],
'max-age' => Cache::PERMANENT,

);
Tags: Where the render result depends on.
Contexts: What makes the render result vary.
Max age: The max time the render result is valid.
For cache variations
To invalidate cache
Render #cache
‣ tags: Identifiers of rendered content. Used
for cache invalidation.

Example: node:42, user_view,
config:image.style.large

‣ contexts: Contexts by which the content
may vary.

‣ max-age: The maximum time cached
content is valid. Defaults to permanent.
14
#cache context
‣ A string that refers to one of the available
cache context services.

‣ Examples:

‣ https://www.drupal.org/developing/api/8/
cache/contexts
15
languages
:type
route
.menu_active_trails
:menu_name
.name
session
theme
timezone
user
.is_super_user
.node_grants
:operation
.permissions
.roles
:role
Contexts may be hierarchical:
parent.child
#cache tags
16
class AuthorNameFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items) {

$elements = array();



foreach ($items as $delta => $item) {

/** @var $comment DrupalcommentCommentInterface */

$comment = $item->getEntity();

$account = $comment->getOwner();

$elements[$delta] = array(

'#theme' => 'username',

'#account' => $account,

'#cache' => array(

'tags' => $account->getCacheTags() + $comment->getCacheTags(),

),

);

}
 Cache tags for this render element.
Cache should respond to changes in
account or comment.
Field formatter for comment author name.
#cache contexts, etc.
17
class CommentForm extends ContentEntityForm {

public function form(array $form, FormStateInterface $form_state) {
/** @var DrupalcommentCommentInterface $comment */

$comment = $this->entity;

$entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())

$field_name = $comment->getFieldName();

$field_definition = $this->entityManager->getFieldDefinitions($entity

$config = $this->config('user.settings');



// In several places within this function, we vary $form on:

// - The current user's permissions.

// - Whether the current user is authenticated or anonymous.

// - The 'user.settings' configuration.

// - The comment field's definition.

$form['#cache']['contexts'][] = 'user.permissions';

$form['#cache']['contexts'][] = 'user.roles:authenticated';

$this->renderer->addCacheableDependency($form, $config);

$this->renderer->addCacheableDependency($form, $field_definition->getConfig($entity-
>bundle()));

Add cache contexts for users
The comment form depends on:
- comment field settings
- user settings
Add cache tags and contexts from the
configuration this form depends on.
#cache: keys
‣ #cache ‘keys’ are used for the database row ID.

‣ Most tags are created from ‘contexts’.

‣ Some tags are added.
18
entity_view:block:bartik_main_menu:[languages:language_interface]=en:
[languages:language_url]=en:[route.menu_active_trails:main]=menu_trail.main|:
[theme]=bartik:
[user.permissions]=120570f7a1954c488d57d2aac1dbc0a48b5072160533431c4ec9f19aa7602e86
#cache contexts:
• languages:language_interface
• languages:language_url
• route.menu_active_trails:main
• theme
• user.permissions
Added keys:
• entity_view
• block
• bartik_main_menu
Main menu block ‘cid’

taken from table cache_render
#cache bubbling
‣ Cacheability bubbles up during rendering.

‣ Child’s #cache properties are combined into
the parent’s #cache properties.

‣ Helper functions in Renderer class.
19
Thought process
20
Am I rendering
something?
yes ['#cache']['contexts']
Does the output
become outdated
after a certain time?
Does the output
vary by
something?
Does the output
depend on
something?
['#cache']['tags']
['#cache']['max-age']
yes
yes
yes
Demo !
21
Demo: Recent content
A simple list of links to recent content.

Repo: https://github.com/wizzlern/cacheit
22
Provided #cache data
‣ Content entities, configuration, block-,
context-, condition-plugins, etc.

‣ Any objects that implement:
CacheableDependencyInterface
23
interface CacheableDependencyInterface {

public function getCacheContexts();

public function getCacheTags();

public function getCacheMaxAge();

}
Renderer::addCacheableDependency($build, $dependency)
Demo: Dynamic Ad
An advertisement block with dynamic content.

Repo: https://github.com/wizzlern/cacheit
24
Placeholders
‣ Placeholders allow separation of cachable (‘static’)
and non-cacheable (‘dynamic’) content.

‣ Lazy builder is a service.

‣ Builder arguments only integer, float, string, boolean.
25
if ($display->getComponent('links')) {

$build[$id]['links'] = array(

'#lazy_builder' => ['comment.lazy_builders:renderLinks', [

$entity->id(),

$view_mode,

$entity->language()->getId(),

!empty($entity->in_preview),

]],

'#create_placeholder' => TRUE,

);

}
Placeholders
‣ Other placeholder data:

‣ css, js header, js footer

‣ Auto placeholdering

‣ When #cache properties are below a threshold

‣ default: max-age: 0, contexts: ['session', ‘user']

‣ Configuration:
renderer.config:auto_placeholder_conditions
26
Invalidation process
‣ Cache invalidation per tag

‣ +1 ‘invalidation’ in cachetags table

‣ Calculate the sum of invalidation of all tags.

‣ Compare with ‘checksum’ value in cache_* table.

‣ Stale data remains in the cache_* table until
overwritten by new data.

‣ See DatabaseCacheTagsChecksum class
27
Debugging
‣ Check cache metadata at page level:

‣ http.response.debug_cacheability_headers : true

(in sites/*/services.yml)

‣ Check cache metadata at any level:

‣ Renderviz module (still needs love)

‣ Database table cache_render

‣ Test with different user roles !!
28
Summary
29
Resources
‣ Classes:

‣ DrupalCoreCacheCache

‣ DrupalCoreRenderRenderer

‣ Documentation

‣ https://drupal.org/developing/api/8/cache

‣ http://drupal.org/developing/api/8/render/arrays/
cacheability
30
Links
‣ https://events.drupal.org/barcelona2015/
sessions/making-drupal-fly-fastest-drupal-
ever-here

‣ https://events.drupal.org/barcelona2015/
sessions/caching-edge-cdns-everyone
31
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
32
http://wizzlern.nl
@wizzlern

Contenu connexe

Tendances

[drupalday2017] - Speed-up your Drupal instance!
[drupalday2017] - Speed-up your Drupal instance![drupalday2017] - Speed-up your Drupal instance!
[drupalday2017] - Speed-up your Drupal instance!DrupalDay
 
Device Synchronization with Javascript and PouchDB
Device Synchronization with Javascript and PouchDBDevice Synchronization with Javascript and PouchDB
Device Synchronization with Javascript and PouchDBFrank Rousseau
 
GR8Conf 2011: Building Progressive UIs with Grails
GR8Conf 2011: Building Progressive UIs with GrailsGR8Conf 2011: Building Progressive UIs with Grails
GR8Conf 2011: Building Progressive UIs with GrailsGR8Conf
 
Relevance trilogy may dream be with you! (dec17)
Relevance trilogy  may dream be with you! (dec17)Relevance trilogy  may dream be with you! (dec17)
Relevance trilogy may dream be with you! (dec17)Woonsan Ko
 
Karine bosch caching-spsbe14
Karine bosch caching-spsbe14Karine bosch caching-spsbe14
Karine bosch caching-spsbe14BIWUG
 
Mobile & Desktop Cache 2.0: How To Create A Scriptable Cache
Mobile & Desktop Cache 2.0: How To Create A Scriptable CacheMobile & Desktop Cache 2.0: How To Create A Scriptable Cache
Mobile & Desktop Cache 2.0: How To Create A Scriptable CacheBlaze Software Inc.
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalabilityTwinbit
 
N hidden gems in forge (as of may '17)
N hidden gems in forge (as of may '17)N hidden gems in forge (as of may '17)
N hidden gems in forge (as of may '17)Woonsan Ko
 
Level Up: 5 Expert Tips for Optimizing WordPress Performance
Level Up: 5 Expert Tips for Optimizing WordPress PerformanceLevel Up: 5 Expert Tips for Optimizing WordPress Performance
Level Up: 5 Expert Tips for Optimizing WordPress PerformancePantheon
 
Drupal8 migrate
Drupal8 migrateDrupal8 migrate
Drupal8 migrateJohn Doyle
 
Render Caching for Drupal 8
Render Caching for Drupal 8Render Caching for Drupal 8
Render Caching for Drupal 8John Doyle
 
Challenges when building high profile editorial sites
Challenges when building high profile editorial sitesChallenges when building high profile editorial sites
Challenges when building high profile editorial sitesYann Malet
 
Hong Kong Drupal User Group - Sep 13th
Hong Kong Drupal User Group - Sep 13thHong Kong Drupal User Group - Sep 13th
Hong Kong Drupal User Group - Sep 13thWong Hoi Sing Edison
 

Tendances (18)

[drupalday2017] - Speed-up your Drupal instance!
[drupalday2017] - Speed-up your Drupal instance![drupalday2017] - Speed-up your Drupal instance!
[drupalday2017] - Speed-up your Drupal instance!
 
Device Synchronization with Javascript and PouchDB
Device Synchronization with Javascript and PouchDBDevice Synchronization with Javascript and PouchDB
Device Synchronization with Javascript and PouchDB
 
GR8Conf 2011: Building Progressive UIs with Grails
GR8Conf 2011: Building Progressive UIs with GrailsGR8Conf 2011: Building Progressive UIs with Grails
GR8Conf 2011: Building Progressive UIs with Grails
 
Relevance trilogy may dream be with you! (dec17)
Relevance trilogy  may dream be with you! (dec17)Relevance trilogy  may dream be with you! (dec17)
Relevance trilogy may dream be with you! (dec17)
 
Grails and Neo4j
Grails and Neo4jGrails and Neo4j
Grails and Neo4j
 
Karine bosch caching-spsbe14
Karine bosch caching-spsbe14Karine bosch caching-spsbe14
Karine bosch caching-spsbe14
 
Webpack
WebpackWebpack
Webpack
 
5 critical-optimizations.v2
5 critical-optimizations.v25 critical-optimizations.v2
5 critical-optimizations.v2
 
Mobile & Desktop Cache 2.0: How To Create A Scriptable Cache
Mobile & Desktop Cache 2.0: How To Create A Scriptable CacheMobile & Desktop Cache 2.0: How To Create A Scriptable Cache
Mobile & Desktop Cache 2.0: How To Create A Scriptable Cache
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalability
 
N hidden gems in forge (as of may '17)
N hidden gems in forge (as of may '17)N hidden gems in forge (as of may '17)
N hidden gems in forge (as of may '17)
 
your browser, my storage
your browser, my storageyour browser, my storage
your browser, my storage
 
Level Up: 5 Expert Tips for Optimizing WordPress Performance
Level Up: 5 Expert Tips for Optimizing WordPress PerformanceLevel Up: 5 Expert Tips for Optimizing WordPress Performance
Level Up: 5 Expert Tips for Optimizing WordPress Performance
 
Always on! Or not?
Always on! Or not?Always on! Or not?
Always on! Or not?
 
Drupal8 migrate
Drupal8 migrateDrupal8 migrate
Drupal8 migrate
 
Render Caching for Drupal 8
Render Caching for Drupal 8Render Caching for Drupal 8
Render Caching for Drupal 8
 
Challenges when building high profile editorial sites
Challenges when building high profile editorial sitesChallenges when building high profile editorial sites
Challenges when building high profile editorial sites
 
Hong Kong Drupal User Group - Sep 13th
Hong Kong Drupal User Group - Sep 13thHong Kong Drupal User Group - Sep 13th
Hong Kong Drupal User Group - Sep 13th
 

Similaire à Drupal 8 Render Cache

Drupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodDrupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodPiyuesh Kumar
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalystsvilen.ivanov
 
Symfony HTTP Kernel for refactoring legacy apps: the eZ Publish case study - ...
Symfony HTTP Kernel for refactoring legacy apps: the eZ Publish case study - ...Symfony HTTP Kernel for refactoring legacy apps: the eZ Publish case study - ...
Symfony HTTP Kernel for refactoring legacy apps: the eZ Publish case study - ...Gaetano Giunta
 
8 things to know about theming in drupal 8
8 things to know about theming in drupal 88 things to know about theming in drupal 8
8 things to know about theming in drupal 8Logan Farr
 
AFUP Lorraine - Symfony Webpack Encore
AFUP Lorraine - Symfony Webpack EncoreAFUP Lorraine - Symfony Webpack Encore
AFUP Lorraine - Symfony Webpack EncoreEngineor
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJames Casey
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedGil Fink
 
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
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 
Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ride on the Fast Track of Web with Ruby on Rails- Part 2Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ride on the Fast Track of Web with Ruby on Rails- Part 2A.K.M. Ahsrafuzzaman
 
Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...
Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...
Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...DrupalCamp Kyiv
 
ITB2019 Containerizing ContentBox CMS - Gavin Pickin
ITB2019 Containerizing ContentBox CMS - Gavin PickinITB2019 Containerizing ContentBox CMS - Gavin Pickin
ITB2019 Containerizing ContentBox CMS - Gavin PickinOrtus Solutions, Corp
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and DesktopElizabeth Smith
 
Caching Strategies for Scaling Drupal: Common Missteps vs Best Practices
Caching Strategies for Scaling Drupal: Common Missteps vs Best PracticesCaching Strategies for Scaling Drupal: Common Missteps vs Best Practices
Caching Strategies for Scaling Drupal: Common Missteps vs Best PracticesAcquia
 
Single Page JavaScript WebApps... A Gradle Story
Single Page JavaScript WebApps... A Gradle StorySingle Page JavaScript WebApps... A Gradle Story
Single Page JavaScript WebApps... A Gradle StoryKon Soulianidis
 
Containerizing ContentBox CMS
Containerizing ContentBox CMSContainerizing ContentBox CMS
Containerizing ContentBox CMSGavin Pickin
 
How to Webpack your Django!
How to Webpack your Django!How to Webpack your Django!
How to Webpack your Django!David Gibbons
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Hugo Hamon
 
Building high performance web apps.
Building high performance web apps.Building high performance web apps.
Building high performance web apps.Arshak Movsisyan
 

Similaire à Drupal 8 Render Cache (20)

Drupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodDrupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hood
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalyst
 
Symfony HTTP Kernel for refactoring legacy apps: the eZ Publish case study - ...
Symfony HTTP Kernel for refactoring legacy apps: the eZ Publish case study - ...Symfony HTTP Kernel for refactoring legacy apps: the eZ Publish case study - ...
Symfony HTTP Kernel for refactoring legacy apps: the eZ Publish case study - ...
 
8 things to know about theming in drupal 8
8 things to know about theming in drupal 88 things to know about theming in drupal 8
8 things to know about theming in drupal 8
 
AFUP Lorraine - Symfony Webpack Encore
AFUP Lorraine - Symfony Webpack EncoreAFUP Lorraine - Symfony Webpack Encore
AFUP Lorraine - Symfony Webpack Encore
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
 
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
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ride on the Fast Track of Web with Ruby on Rails- Part 2Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ride on the Fast Track of Web with Ruby on Rails- Part 2
 
Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...
Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...
Anton Faibyshev - Drupal 8: lazy builder. What we need to build a house - we ...
 
ITB2019 Containerizing ContentBox CMS - Gavin Pickin
ITB2019 Containerizing ContentBox CMS - Gavin PickinITB2019 Containerizing ContentBox CMS - Gavin Pickin
ITB2019 Containerizing ContentBox CMS - Gavin Pickin
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and Desktop
 
Caching Strategies for Scaling Drupal: Common Missteps vs Best Practices
Caching Strategies for Scaling Drupal: Common Missteps vs Best PracticesCaching Strategies for Scaling Drupal: Common Missteps vs Best Practices
Caching Strategies for Scaling Drupal: Common Missteps vs Best Practices
 
Single Page JavaScript WebApps... A Gradle Story
Single Page JavaScript WebApps... A Gradle StorySingle Page JavaScript WebApps... A Gradle Story
Single Page JavaScript WebApps... A Gradle Story
 
Local storage
Local storageLocal storage
Local storage
 
Containerizing ContentBox CMS
Containerizing ContentBox CMSContainerizing ContentBox CMS
Containerizing ContentBox CMS
 
How to Webpack your Django!
How to Webpack your Django!How to Webpack your Django!
How to Webpack your Django!
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2
 
Building high performance web apps.
Building high performance web apps.Building high performance web apps.
Building high performance web apps.
 

Dernier

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
[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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
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 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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Dernier (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
[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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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 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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Drupal 8 Render Cache

  • 1.
  • 4. Wizzlern ‣ 100% Drupal ‣ Drupal training and consultancy ‣ Front-, backend-development ‣ Site administration and editorial ‣ Advanced subjects ‣ Since 2009 ‣ Active in the Drupal community ‣ Visit us at http://wizzlern.nl 4
  • 6. Drupal 8 Render Cache ‣ D7-D8 differences ‣ Cache terminology ‣ The thought process ‣ Demonstration: ‣ Recent content list ‣ Dynamic advertisement 6
  • 8. “There  are  only  two  hard   things  in  Computer  Science:  
 cache  invalidation  and   naming  things.” Phil Karlton 8
  • 9. Drupal 7 - Page cache ‣ Core: Cache at page level. ‣ Low complexity cache invalidation. ‣ When a page is edited, the whole page cache is cleared. ‣ When a comment is added, the whole page cache is cleared. 9
  • 10. Drupal 7 - Contrib cache ‣ Cache Expiration and various Varnish modules: High complexity cache invalidation: ‣ purge / expire all URLs that contains the changing content or is referring to it. ‣ High invalidation costs. 10
  • 11. Drupal 8 Render Cache 11
  • 12. Drupal 8 strategy ‣ High-complexity cache invalidation ‣ Content invalidated instantaneously ‣ Cached permanently ‣ Cache chaining ‣ Rendered data cached at multiple levels: ‣ Page, Block, Entity (node, comment, user, etc.), View, Menu tree, Text Format filter result, etc. 12
  • 13. Render #cache 13 $build['#cache'] = array(
 'tags' => ['block_view', 'config:block.block.recentcontent'],
 'contexts' => ['languages:language_interface', 'user.permissions'], 'max-age' => Cache::PERMANENT,
 ); Tags: Where the render result depends on. Contexts: What makes the render result vary. Max age: The max time the render result is valid. For cache variations To invalidate cache
  • 14. Render #cache ‣ tags: Identifiers of rendered content. Used for cache invalidation.
 Example: node:42, user_view, config:image.style.large ‣ contexts: Contexts by which the content may vary. ‣ max-age: The maximum time cached content is valid. Defaults to permanent. 14
  • 15. #cache context ‣ A string that refers to one of the available cache context services. ‣ Examples:
 ‣ https://www.drupal.org/developing/api/8/ cache/contexts 15 languages :type route .menu_active_trails :menu_name .name session theme timezone user .is_super_user .node_grants :operation .permissions .roles :role Contexts may be hierarchical: parent.child
  • 16. #cache tags 16 class AuthorNameFormatter extends FormatterBase { public function viewElements(FieldItemListInterface $items) {
 $elements = array();
 
 foreach ($items as $delta => $item) {
 /** @var $comment DrupalcommentCommentInterface */
 $comment = $item->getEntity();
 $account = $comment->getOwner();
 $elements[$delta] = array(
 '#theme' => 'username',
 '#account' => $account,
 '#cache' => array(
 'tags' => $account->getCacheTags() + $comment->getCacheTags(),
 ),
 );
 }
 Cache tags for this render element. Cache should respond to changes in account or comment. Field formatter for comment author name.
  • 17. #cache contexts, etc. 17 class CommentForm extends ContentEntityForm {
 public function form(array $form, FormStateInterface $form_state) { /** @var DrupalcommentCommentInterface $comment */
 $comment = $this->entity;
 $entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())
 $field_name = $comment->getFieldName();
 $field_definition = $this->entityManager->getFieldDefinitions($entity
 $config = $this->config('user.settings');
 
 // In several places within this function, we vary $form on:
 // - The current user's permissions.
 // - Whether the current user is authenticated or anonymous.
 // - The 'user.settings' configuration.
 // - The comment field's definition.
 $form['#cache']['contexts'][] = 'user.permissions';
 $form['#cache']['contexts'][] = 'user.roles:authenticated';
 $this->renderer->addCacheableDependency($form, $config);
 $this->renderer->addCacheableDependency($form, $field_definition->getConfig($entity- >bundle()));
 Add cache contexts for users The comment form depends on: - comment field settings - user settings Add cache tags and contexts from the configuration this form depends on.
  • 18. #cache: keys ‣ #cache ‘keys’ are used for the database row ID. ‣ Most tags are created from ‘contexts’. ‣ Some tags are added. 18 entity_view:block:bartik_main_menu:[languages:language_interface]=en: [languages:language_url]=en:[route.menu_active_trails:main]=menu_trail.main|: [theme]=bartik: [user.permissions]=120570f7a1954c488d57d2aac1dbc0a48b5072160533431c4ec9f19aa7602e86 #cache contexts: • languages:language_interface • languages:language_url • route.menu_active_trails:main • theme • user.permissions Added keys: • entity_view • block • bartik_main_menu Main menu block ‘cid’
 taken from table cache_render
  • 19. #cache bubbling ‣ Cacheability bubbles up during rendering. ‣ Child’s #cache properties are combined into the parent’s #cache properties. ‣ Helper functions in Renderer class. 19
  • 20. Thought process 20 Am I rendering something? yes ['#cache']['contexts'] Does the output become outdated after a certain time? Does the output vary by something? Does the output depend on something? ['#cache']['tags'] ['#cache']['max-age'] yes yes yes
  • 22. Demo: Recent content A simple list of links to recent content. Repo: https://github.com/wizzlern/cacheit 22
  • 23. Provided #cache data ‣ Content entities, configuration, block-, context-, condition-plugins, etc. ‣ Any objects that implement: CacheableDependencyInterface 23 interface CacheableDependencyInterface {
 public function getCacheContexts();
 public function getCacheTags();
 public function getCacheMaxAge();
 } Renderer::addCacheableDependency($build, $dependency)
  • 24. Demo: Dynamic Ad An advertisement block with dynamic content. Repo: https://github.com/wizzlern/cacheit 24
  • 25. Placeholders ‣ Placeholders allow separation of cachable (‘static’) and non-cacheable (‘dynamic’) content. ‣ Lazy builder is a service. ‣ Builder arguments only integer, float, string, boolean. 25 if ($display->getComponent('links')) {
 $build[$id]['links'] = array(
 '#lazy_builder' => ['comment.lazy_builders:renderLinks', [
 $entity->id(),
 $view_mode,
 $entity->language()->getId(),
 !empty($entity->in_preview),
 ]],
 '#create_placeholder' => TRUE,
 );
 }
  • 26. Placeholders ‣ Other placeholder data: ‣ css, js header, js footer ‣ Auto placeholdering ‣ When #cache properties are below a threshold ‣ default: max-age: 0, contexts: ['session', ‘user'] ‣ Configuration: renderer.config:auto_placeholder_conditions 26
  • 27. Invalidation process ‣ Cache invalidation per tag ‣ +1 ‘invalidation’ in cachetags table ‣ Calculate the sum of invalidation of all tags. ‣ Compare with ‘checksum’ value in cache_* table. ‣ Stale data remains in the cache_* table until overwritten by new data. ‣ See DatabaseCacheTagsChecksum class 27
  • 28. Debugging ‣ Check cache metadata at page level: ‣ http.response.debug_cacheability_headers : true
 (in sites/*/services.yml) ‣ Check cache metadata at any level: ‣ Renderviz module (still needs love) ‣ Database table cache_render ‣ Test with different user roles !! 28
  • 30. Resources ‣ Classes: ‣ DrupalCoreCacheCache ‣ DrupalCoreRenderRenderer ‣ Documentation ‣ https://drupal.org/developing/api/8/cache ‣ http://drupal.org/developing/api/8/render/arrays/ cacheability 30
  • 32. This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 32 http://wizzlern.nl @wizzlern