Actually you don't need to specify key for @Cacheable at all
In my case I needed to read from repo a collection and store it in-memory for the following iterations through a list.
@Cacheable("users")
public List<RegionPercentage> getUserByIds(List<Long> accountIds) {
return repository.getUsers();
}
Using this method my method returns list from cache every time it is NOT empty INSTEAD of invoking repository.
Thus, for the sake of updating cache I managed to clear it automatically using cron (in my case every 30 seconds to see the difference)
@Scheduled(cron = "0,30 * * * * ?")
@CacheEvict(value = "users", allEntries = true)
public void deleteCache() {
// this method can be empty
}
Do not forget to set up @EnableCaching in main class of your Spring Boot Application