I'm having trouble with @Cacheable using with multi parameters and pagination.
@Cacheable(value = "books", key = "#p0")
public List<Book> findBooks(Long loggedUserId, String q, Integer firstResult, Integer maxResults) {
...
return result;
}
The problem is when I call the method by the first time, the content is cached with success and the result is like I expect. But when I call by the second time, the result returned is equals to the first time. If I disable the cache or remove the key the results are different.
The use of the key is obligatory because sometimes the cache of a specified user is removed.
Thanks.
I think your issue is a lack of understanding of the Cacheable annotation, and you you are using it. So let me see if I can help
Your annotation is @Cacheable(value = "books", key = "#p0")
This means that when this method is called it will take the first parameter and look it up in the cache and if there is a result, it will be returned instead of executing the method. IT IS ONLY CHECKING THE FIRST PARAMETER.
Your cache key needs to be something that uniquely identifies the result set. In this case user id is common across multiple results, and does not uniquely identify a page.
You know your use case better than I, but something like this would probably be better:
The above will cache based on the search query and the index of the page (start and end of results). Since I don't know your use case I don't know why user id is in there, but from the information I have those three should uniquely identify a result page.