How to Use Spring Cache for More Efficient Data Access
Caching has been a tech trend in recent years to improve application performance. In this article, we will walk through the common cache annotations including Cacheable, CachePut and CacheEvict under Spring Cache framework, with a continuous code flow.
Import Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
@ Cacheable
Used to mark a method or a class. If mark a method, cache the return value of the method. If mark a class, cache all the return values of the class methods
In the following example, we will query for itemId based on the userId and store the result as the cache data.
- value = Name(s) of the cache(s)
- key = rule of the key generated, written using springEL
The Cacheable annotation allows the method to execute if the data is not present in the cache. If the data is present in the cache, it will…