With the following Controller method:
@GetMapping("/books")
public ResponseEntity<List<Book>> getBooksByTitleOrAuthor(
@RequestParam(required = false) String title,
@RequestParam(required = false) String author) {
if ((title== null || title.isEmpty()) && (author == null || author.isEmpty()))
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
if (author != null) {
Logger.info("GET /books?author=" + author);
return bookService.getByAuthor(author)
}
Logger.info("GET /books?title=" + title);
return bookService.getByTitle(title));
}
I can call the endpoint with:
/books?title=SomeBookTitle
or
/books?author=SomeAuthor
How can I set the Redis key with either title
or author
here using @Cacheable
?
2
Answers
You may try to use the cache SpEL metadata such as the name of the method being invoked as follows:
cacheManager : name of bean which handles cache requests