What makes the Cache API in Drupal 8 any better than Drupal 7's cache system? What's so revolutionary about it? Which of the old limitations does it remove? What are those new concepts and terminology that you should learn about?

And, most of all: how complex is it to set up a cache in Drupal 8 for a specific use case?

You might have already bumped into terms like “max-age”, "context cache" or "cache tags".
 

But how precisely do these new concepts, part of Drupal 8's cache system, refine and streamline the way you cache data on your website?
 

Let's try to demystify the terminology of Drupal 8's Cache API and to translate its new “fancy” terminology into... crystal-clear benefits for you:
 

1. What Is Caching More Precisely? Why Do We Cache Data?

To your “What” question I'd answer:
 

Caching is a... strategy (or layer) for storing data from your website. Or: it's a software or hardware component where you store your data. 
 

Why would you want to store your data?
 

Because this will streamline the way your website serves all future requests for that cached data.
 

And it goes without saying that reading data straight from the cache takes less time than... retrieving it from a slower data container or fully recreating the result.

In short: caching data translates into faster page load time.


2. Cache API in Drupal 8: The Automatic Cache System

A brief, yet accurate definition of cache in Drupal 8 would be:
 

Storing data that takes too long to load.
 

And if I am to detail it a bit I'd have to add that:
 

Caching can be either permanent or time-limited and that you're free to cache any type of data on your website.
 

Now, talking about Drupal 8's cache API, what everyone points out is that: it is much improved. That it's so different from the cache systems of the previous Drupal versions that... you even risk turning your website uncachable if you're not familiar with its new concepts.
 

“But how different/sophisticated can it be?” you might ask yourself.
 

Before we delve deep into details let me add just one thing:
 

We're talking about an... automated cache system. Basically, your Drupal 8 website retrieves cache data for both anonymous and logged in users with no configuration whatsoever. All by default.
 

And now, let's shed some light on all these new fancy concepts that the Cache API in Drupal 8 is based on:
 

2.1.The Cache Tags

We all do agree that “invalidating cache” is one of the most challenging tasks of any cache system.

Luckily, not anymore. At least not in Drupal 8, where you now have the concept of “cache tags” that you can use for tagging:
 

  • specific pages
  • specific page elements
  • various types of content
     

… and thus invalidate them all. Improved efficiency and high accuracy through... basic tagging.

Basically, using these cache tags you can easily identify outdated data stored in multiple cache bins and... invalidate it.

This way, you no longer run the risk of invalidating “still green” cache items, in bulk, not knowing which data to invalidate.
 

2.2. The Context Cache

Here's an all too common scenario:
 

You're faced with multiple variants of the same data; only one of them should be cached, based on a specific criterion like language, user, country, content access permission...
 

Well, how do you automate targetting the right variant to be cached? And how do you automate caching the other left variants, as well, depending on the... context.

You use “cache contexts”, that's how...

They're one of those new remarkable features that the Cache API in Drupal 8 ships with, that allow you to specify the criteria to be used to vary for the cached content on a page. By user, by language, by country, by path...
 

2.3. The Max-Age (The Cache Duration)

Maybe you don't want certain data to be forever cached. Maybe you need it stored for a certain period of time only.

In this respect, the “max-age” property in Drupal 8's cache system allows you to define that time limit. To invalidate data that will have run... out of time.
 

2.4. The Bubbleable Cache Metadata

What does this even mean “cache metadata... bubbling”?

Let's take this example: 
 

You have a parent item with its own “family” of... children items. In this context, “bubbled tags” makes it possible for the parent item in this render array to receive cacheability metadata from its children.
 

Bubble cache metadata streamlines the whole process of invalidating outdated cached data. As simple as that...
 

The END!

Is it any clearer for you now what makes the Cache API in Drupal 8 so powerful? How its new features come to remove most of the limitations that you've already faced in Drupal 7?

And how you can use them to refine and automate caching on your own Drupal 8 website?


Image by Pexels from Pixabay  

Development

We do Drupal development

Go to our Drupal page!

Visit page!

Browse cities

Recommended Stories

Drupal: Age 1-11 in a Nutshell
For over two decades, Drupal has evolved from a simple message board to one of the most powerful and versatile… (Read more)
10 minutes /
Drupal Security Best Practices: Protecting Your Website from Common Threats
As Drupal and other technologies have grown, so have the stakes for keeping websites secure. Security isn’t a… (Read more)
10 minutes /
The Future of Drupal: What’s Next in Web Technologies
The digital world never sits still, and neither should your website. As users demand faster, smarter, and more… (Read more)
10 mins /