Modern websites are much more dynamic and interactive than 10 years ago, making it more difficult to build modern sites while also being fast
Dries Buytaert
Drupal founder and project lead,
President Drupal Association,
Acquia co-founder and CTO
Typically we return
cache tags
cache contexts
cache max-age
Clear cache bin
Clear cache item
Prefix based invalidation
Drupal 7:
Clear the entire cache bin.
Cache tags = data dependencies
Cache tags describe dependencies on data managed by Drupal
There are only two hard things in Computer Science: cache invalidation and naming things.
Phil Karton
cache_tags.invalidator service
Cache contexts = (request) context dependencies
Cache contexts are analogous to HTTP's Vary header.
Data is context-dependent variation.
A cache context is a string that refers to one of the available cache context services.
Cache contexts are hierarchical.
Drupal 8 core ships with the following hierarchy of cache contexts:
cookies
:name
headers
:name
ip
languages
:type
request_format
route
.book_navigation
.menu_active_trails
:menu_name
.name
session
.exists
theme
timezone
url
.host
.query_args
:key
.pagers
:pager_id
.path
.site
user
.is_super_user
.node_grants
:operation
.permissions
.roles
:roleoptimize([user, user.permissions]) = [user]
Results:
Solution:
user.node_grants
max-age = 0
optimize([user, user.node_grants]) =
[user, user.node_grants]
user.node_grants
max-age = 3600
optimize([user, user.node_grants]) =
[user]
- cache.context tagged services
- implements \Drupal\Core\Cache\Context\CacheContextInterface
or \Drupal\Core\Cache\Context\CalculatedCacheContextInterface
cache_context.route.book_navigation:
class: Drupal\book\Cache\BookNavigationCacheContext
arguments: ['@request_stack']
tags:
- { name: cache.context}cache_context (mandatory prefix) + route (parents) + book_navigation (this cache context's name)
X-Drupal-Cache-Contexts header
Don't you see it ?
Set http.response.debug_cacheability_headers to true in services.yml
Cache max-age = time dependencies
Cache max-age is analogous to HTTP's Cache-Control header's max-age directive
Cache max-age provides a declarative way to create time-dependent caches.
A cache max-age is a positive integer, expressing a number of seconds.
Cache max-ages are passed around as individual integers, because a given cache item can only logically have a single max-age.