Application performance has always been one of the pain points when performing development with Drupal, and there are many reasons for this. For example, PHP is not the fastest language out there. Many beginner Drupal developers fell pray to the multitude of modules available and go a bit overboard with enabling more than needed. Indeed, the Drupal architecture is simply not the most performant. In its defense though,it is a very complex architecture that does a lot out of the box will have some speed trade-offs.
One critical component in this game, however, is caching. For those of you not familiar with this term, caching is the application strategy of storing copies of processed code (or anything that results from it) in view of delivering it faster to the user when requested subsequent times. For example, when you go to a website, you browser is most likely going to cache (store) certain assets locally on your computer so that when you visit the site the next time, it can show them to you faster.
Although caching has been steadily improving with recent versions of Drupal, it was still lacking significantly. Particularly when it comes to serving registered users. Drupal 8, however, is a completely different ball game. The system has been totally revamped and has all aspects of the Drupal architecture. Unfortunately, though, this has put yet another big new thing on the plate of things Drupal 7 developers need to learn. Because it's a complex system, we simply cannot (and should not) get around it. But you're in luck, because in this chapter we will break it all down and see what we're dealing with. So when you are doing module development in Drupal 8, your code will be more performant, your site will run faster, and ultimately your users will be happier.
So, what exactly are we going to talk about in this chapter?
First, we are going to cover some introductory notions about the caching system in Drupal 8 and look at the main types of caching available. Here, we will also see how, during development, we can disable caching to increase our productivity.
Next, we are going to talk about cacheability metadata. This is one of the most important things you'll need to know as a Drupal 8 module developer when it comes to caching. It has to do with declaring render arrays (and other objects) in a way in which Drupal can cache them properly (and invalidate the caches accordingly). We will talk about things such as cache tags, contexts, and max-age, but also see how to apply them to render arrays, block plugins, and access results.
After that, we will look at how we can tackle highly dynamic components (render arrays), which cannot or should not be cached. Drupal 8 has a powerful auto-placeholdering system that uses lazy builders to postpone rendering until a later stage, which can greatly improve both cacheability and perceived performance.
Lastly, we are going to look at how we can interact with the Cache API ourselves in order to create, read, and invalidate our own cache entries. Sometimes we need to perform expensive calculations or show external data on our site, which can benefit from being cached.
So let's get to it.