0

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;
}
ajms
  • 67
  • 7

1 Answers1

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.

John Blum
  • 7,381
  • 1
  • 20
  • 30