A lot of the processes that Drupal performs when responding to a request are cached in order to increase performance. Creating the HTML for the page that a user sees, or the JSON response to a REST request, can require thousands of operations. Some operations are time-consuming, memory heavy, CPU intense, or all three. By performing the operation once, and then caching the result for next time, subsequent requests can be fulfilled faster. In order to make it easier to store, retrieve, and invalidate cached data Drupal provides cache-related services you can use in your code. Drupal also lets you provide information about the cacheability of data to the Render API to improve the performance of rendering a page.
- Check if the data you want to cache is already cached
- Add data to be cached
- Set a timespan for how long certain data should remain cached
Drupal's Cache API has been around since the initial release of Drupal 8.0.0 and isn't likely to change anytime soon. Resources labeled with Drupal 8 should also work for Drupal 9.