How to pass key to @Cacheable annotation in a dynamic fashion during pagination

22 Views Asked by At

How to construct cache key for the following call, so that cache is reused for all scenarios while paginating, if I don't use Cacheable annotation will it still annotate by generating keys dynamically. This is the existing code which forms key dynamically

@Cacheable(value = "states", key = "#country + '_' + #pageable.pageNumber + '_' + #pageable.pageSize")
public Page<List<String>> findStatesByName(String country, Pageable pageable) {
    Sort sort = Sort.by(Sort.Order.asc("state"));
    PageRequest pr = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
    Page<List<String>> states = countryRepository.findStatesByName(country, pageable);

    return states;
}
0

There are 0 best solutions below