🖥 Spring Boot Caching 101
Tags: spring, springbok, programming
Published: 2021-07-08 12:46:29

#### 📚 Let’s see everything you should know to start dealing with the #Cache #Abstraction in #Spring Boot. We will have a look at its most useful cache-related annotations, as well as the most relevant cache engines.

### <br>
Slow performance is a recurring and complex problem that developers are often faced with. One of the most common approaches to address such a problem is through caching. Indeed, this mechanism allows achieving a substantial improvement in the performance of any type of application. The problem is that dealing with caching is not an easy task. Luckily, caching is provided by Spring Boot transparently thanks to the Spring Boot Cache Abstraction, which is a mechanism allowing consistent use of various caching methods with minimal impact on the code. Let's see everything you should know to start dealing with it.

First, we will introduce the concept of caching. Then, we will study the most common Spring Boot cache-related annotations, understanding what the most important ones are, where, and how to use them. Next, it will be time to see what are the most popular cache engines supported by Spring Boot at the time of writing. Finally, we will see Spring Boot caching in action through an example.

## What is Caching

Caching is a mechanism aimed at enhancing the performance of any kind of application. It relies on a cache, which can be seen as a temporary fast access software or hardware component that stores data to reduce the time required to serve future requests related to the same data. Dealing with caching is complex, but mastering this concept is practically unavoidable for any developer. If you are interested in delving into caching, understanding what it is, how it works, and what are its most important types, you should follow [this](https://auth0.com/blog/what-is-caching-and-how-it-works/) link first. 

## Getting Started

The Spring Boot [Cache Abstraction](https://docs.spring.io/spring-framework/docs/5.0.0.M5/spring-framework-reference/html/cache.html) does not come with the framework natively but requires a few dependencies. Thankfully, you can easily install all of them by adding the [`spring-boot-starter-cache`](https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cache) to your dependencies.

If you are a Gradle user, add this dependency to your `build.gradle` file:

```
implementation "org.springframework.boot:spring-boot-starter-cache:2.5.0"
```
While if you are a Maven user, add the following dependency to your `pom.xml` file:

```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
    <version>2.5.0</version>
</dependency>
```
In detail, `spring-boot-starter-cache` brings the [`spring-context-support`](https://mvnrepository.com/artifact/org.springframework/spring-context-support) module, which transitively depends on [`spring-context`](https://mvnrepository.com/artifact/org.springframework/spring-context). The latter allows Spring to deal with contexts, also called Spring IoC ([Inversion of Control](https://en.wikipedia.org/wiki/Inversion_of_control)) containers, which are responsible for managing objects in a Spring application. In particular, they are in charge of configuring, instantiating, and assembling Beans by reading configuration files and employing annotations. While the  `spring-context-support` module provides support for integrating third-party cache engines, which will be presented later, into a Spring application.
Follow [this](https://docs.spring.io/spring-framework/docs/5.0.0.M5/spring-framework-reference/html/beans.html) link from the Spring official documentation for further reading on the Spring IoC containers and the bean management.  

## Spring Boot Caching Annotations


[Read more...](https://auth0.com/blog/spring-boot-caching-101/?utm_source=content_synd&utm_medium=sc&utm_campaign=spring)