18
đź–Ą Spring Boot Caching 101
📚 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.
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.
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 link first.
The Spring Boot Cache Abstraction 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
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:
<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
module, which transitively depends on spring-context
. The latter allows Spring to deal with contexts, also called Spring IoC (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 link from the Spring official documentation for further reading on the Spring IoC containers and the bean management.
18