drupal 8 cache: under the hood

44
Drupal 8 Caching Improvements

Upload: piyuesh-kumar

Post on 13-Apr-2017

18 views

Category:

Technology


3 download

TRANSCRIPT

Drupal 8 Caching Improvements

About MeTechnical Architect @ QED42

D8 Developer | Trainer | Contributor

https://drupal.org/u/piyuesh23

Love traveling, reading & internet Gaming.

Topics• Cache limitations in D7

• Cacheability Metadata (cache-tags/contexts)

• Cache Invalidation

• Working with reverse proxies

• Cache bins

• Page cache & Bigpipe

• #uncacheable

Cache limitations in D7

Solution: drupal.org/project/render_cache

Needless Rebuilding & Re-rendering

Page Cache

Add a comment to node/1

Before After

Page Cache: Authenticated

https://www.drupal.org/project/authcache

• cache_clear_all(‘cid_21’, $bin);// Clear data for cache id: cid_21 inside $bin

• cache_clear_all(‘cid_*’, $bin);// Clear data for cache ids starting with: cid_ inside $bin

• cache_clear_all(‘*’, $bin);// Clear all cache within cache bin: $bin

Clear any cached content dependent on node/20?

Cache Invalidation

Cacheability Metadata: Cache Tags

Do we need to define/invalidate cache tags for everything manually?

Cache tagsCache tags = {data} dependencies

(Entities/Config/Custom)

Adding cache-tags to render arrays

// Invalidate cache for this render array if anything related to node:20 / user:1 / weather_custom_tag change.

Cache Invalidation: Under the hood

Cache-tag Invalidation: Behind the hood

Updating node: http://dev.d8/node/1/edit

Cache::InvalidateTags([node:1]); Update cachetags set invalidation = invalidation+1 where cachetag = node:1;

Cache-tag Invalidation: ExampleNode:132 Render cache generated

cachetags

Update Node:132 changes to cachetags

Load Node:132

Cache metadata Bubbling

<html>

<region>

<block data-cache-tags=“block:search_form”></block>

</region>

<region>

<block data-cache-tags=“block:news”>

<view data-cache-tags=“view:news”></view>

</block>

<block data-cache-tags=“block:main_content”>

<node data-cache-tags=“node:17”>

<field data-cache-tags=“user:2”></field>

<field data-cache-tags=“filter_format:basic_html file:5 file:3”></field>

</node>

</block

</region>

</html>

<html data-cache-tags=“{all cache tags from regions}”>

<region data-cache-tags=“block:search_form”>

<block data-cache-tags=“block:search_form”></block>

</region>

<region data-cache-tags=“block:news view:news block:main_content node:17 user:2 filter_format:basic_html file:5 file:3”>

<block data-cache-tags=“block:news view:news”>

<view data-cache-tags=“view:news”></view>

</block>

<block data-cache-tags=“block:main_content node:17 user:2 filter_format:basic_html file:5 file:3”>

<node data-cache-tags=“node:17 user:2 filter_format:basic_html file:5 file:3”>

<field data-cache-tags=“user:2”></field>

<field data-cache-tags=“filter_format:basic_html file:5 file:3”></field>

</node>

</block

</region>

</html>

Update node/3

Working with reverse-proxies

X-DRUPAL-CACHE-TAGS headerCreate services.yml file & set http.response.debug_cacheability_headers to true

Varnish demo using purge module

Cacheability Metadata: Cache Contexts

Cache ContextsCan my render array have output variations?

Default cache contexts:

[user.permissions, languages:language_interface. theme]

Cache Contexts: Self Optimized

optimize([user, user.permissions]) = [user]

Cacheability Metadata: Max-Age

Max-AgeWhen should the cache get expired?

Permanent(-1)

Fixed duration({value in seconds})

Don’t Cache(0)

Cache Bins

Cache binsContainer for holding cache with a

configurable backend(could be memcache/redis/{custom cache

backend}).

Default cache-bins:

default: data:discovery:

bootstrap:render:config:

Creating Custom Cache-bins

Using Custom Cache-bins

#Uncacheable

Examples

• Render arrays depending on user context i.e., varies for each user?

• Render arrays having max-age=0

• Render arrays having cache contexts set to either session or user.

• Render arrays that cannot be cached, are converted into placeholders while processing them.

• The markup for these placeholders is calculated based on Render Strategies.

<drupal-render-placeholder callback="timestamp_generator.generator:generateUserTimestamp" arguments="" token="b9dbb057"></drupal-render-placeholder>

Placeholders

Render Strategies• Single Flush strategy:

• DOM built with Cached-content + Placeholders

• All the lazy builders are processed to replace the placeholders with actual content.

P.S: No data is available to the end-user until all the placeholders are processed.

Render Strategies• Big-pipe (N-flush strategy)

• 1 HTML response

• N embedded AJAX responses for each placeholder

Data is presented to end-users as soon as its ready.

TTFB becomes much lower compared to single-flush.

Defining placeholders using #lazy-builders

Do we need to always define placeholders for things that cannot be cached?

Auto-Placeholdering

Services.yml

Thank You!