'Get separate elements with @Cacheable List
I'm using spring cache integration with hazelcast.
I need to get/set(1st call) all the movies list from/to a hazelcast cache map.
I also need to be able to get a specific movie from the cache map.
My problem is that by using List with @Cacheable, it returns a single key (SimpleKey[]) which holds all the movies objects, instead of adding the key/values individually, so I can then use @Cacheable with a key to get a specific map object.
@Cacheable(value = MOVIES_CACHE_NAME, unless = "#result==null or #result.isEmpty()")
public List<Movie> getAllMoviesFromCache() {
log.debug("Going to call getAllMoviesFromCache");
List<Movie> movies = null;
MovieListDetailResponse response = getAllMovies(); //gets from db
if (response != null && response.getMovies() != null) {
movies = convertMovieListResponseToMovie(response.getMovies());
}
log.debug(String.format("Finished getAllMoviesFromCache, got Response %s", response));
return movies;
}
@Cacheable(value = MOVIES_CACHE_NAME, key = "{#movieId}", unless = "#result==null")
public Movie getMovieDetailsFromCache(String movieId) {
Movie movie = null;
MovieDetailResponse response = getMovie(movieId); //gets from db
if (response != null && response.getMovie() != null) {
movie = convertMovieDetailResponseToMovie(response.getMovie());
}
log.info(String.format("Movie by MovieId: '%s'. Response: %s", movieId, movie));
return movie;
}
Solution 1:[1]
See my answer to this similar SO question and specifically have a look at this answer (#2 from my previous answer).
My answers also share example code on how to implement such logic with Spring's Cache Abstraction, see here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Blum |
