Caching
Caching is essential in order to build a performant application. You would not want to hit the external service or the database all the time. Data that does not change frequently can be cached.
Spring provides transparent mechanisms to connect and use a Cache. The following steps are involved in enabling a cache on an application:
- Add the Spring Boot Starter Cache dependency.
- Add caching annotations.
Let's discuss these in detail.
Adding the Spring Boot Starter Cache dependency
The following snippet shows the spring-boot-starter-cache
dependency. Itt brings in all the dependencies and auto-configuration needed to configure a cache:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
Adding caching annotations
The next step is to add the caching annotations, indicating when something needs to be added or removed from the cache. The following snippet shows an example...