Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
Gradle:
groovy
implementation 'org.springframework.boot:spring-boot-starter-cache'
application.properties:
properties
spring.cache.type=ehcache
@Cacheable("books")
public String getBook(String id) {
}
@CachePut("books")
public String updateBook(String id) {
}
@CacheEvict("books")
public void deleteBook(String id) {
}
@Configuration
public class CacheConfig {
@Bean
public KeyGenerator customKeyGenerator() {
return (target, method, params) -> {
StringBuilder key = new StringBuilder();
key.append(target.getClass().getSimpleName());
key.append("_");
key.append(method.getName());
for (Object param : params) {
key.append("_");
key.append(param.toString());
}
return key.toString();
};
}
}
@Service
public class BookService {
@Cacheable("books")
public String getBook(String id) {
}
}