drupal 8 cache: under the hood
TRANSCRIPT
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
Solution: drupal.org/project/render_cache
Needless Rebuilding & Re-rendering
• 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
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-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
<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>
X-DRUPAL-CACHE-TAGS headerCreate services.yml file & set http.response.debug_cacheability_headers to true
Cache ContextsCan my render array have output variations?
Default cache contexts:
[user.permissions, languages:language_interface. theme]
Max-AgeWhen should the cache get expired?
Permanent(-1)
Fixed duration({value in seconds})
Don’t Cache(0)
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:
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.