Summary

In this chapter, we covered the main aspects of caching in Drupal 8 any module developer needs to be familiar with. We introduced some key concepts and talked about the two main types of caching—Internal Page Cache (used for anonymous users) and Dynamic Page Cache (used for authenticated users).

We dug deeper into cacheability metadata, which is probably the most important and common thing we need to understand. It's imperative to use this properly so that all the render arrays we build are cached and invalidated correctly. We also saw how block plugins have specific methods we can use to define their cacheability metadata and how access results should also receive cacheability dependencies, as needed. Stemming from this, we also explored lazy builders and the auto-placeholdering strategies that allow us to handle highly dynamic components while maintaining good cacheability overall.

Lastly, we looked into using the Cache API ourselves in order to store, read, and invalidate our own cache entries. We even saw how to create our own custom cache bin.

Caching is a very important aspect of Drupal 8 module development. In previous versions, it didn't even come close and we were able to get away with it quite often without even paying attention to it. Now, we have a powerful system in place to make rendering more performant, and we should make use of it.

In the next chapter, we are going to talk about JavaScript and how we can use it in a Drupal context, as well as the powerful Ajax API.