By leveraging the unified entity rendering process and cache tags from Drupal 8 and the Render API caching (from Drupal 7), the resulting output of
EntityViewBilder::viewMultiple() is now cached by default.
How it works
When an entity goes through the rendering process, cache tags that are relevant to that entity are added to the render array. What does relevant mean in this context? Well, the ID of the entity and also the IDs of all the other entities that are referenced by it. For example: when a node is rendered, the cache tags will include the node ID and also the ID of node's author (a User entity). If the node contains another reference field targeting, lets say, a taxonomy term, the ID of that term will also be added.
The reason for gathering referenced entity IDs is for cache clearing purposes (which, as they say, is never easy). Thanks to this process, the render cache for an entity can be cleared automatically not only when it is changed (saved or deleted) but also when a referenced entity is changed. For example: if a user changes his username, the cache will be cleared for all the nodes that are authored by him. Likewise, when the name of a taxonomy term is changed, all the nodes that might be tagged with that term must also be updated.
Manual cache clearing
The entity render cache can also be cleared manually, with one of the following options:
- per entity type:
- just for a specific set of entities:
Disabling the render cache
The render cache can disabled globally per entity type by setting the
'render_cache' flag to
FALSE, or individually per view mode, by settings the
'cache' property to
Example for an entity type:
* My awesome entity.
* id = "awesome_entity",
* render_cache = FALSE,
Example for a view mode:
label: Full content
$reset argument has been added to
entity_view_multiple(), for easier rendering fresh (not cached) output of an entity.
Fill the cache correctly: prevent user-specific data