2
@Cacheable(value = "moviesCache")
public Collection<Movies> getMovies() {
    Collection<Movies> moviesList = new ArrayList<>();
    
    //populate moviesList by going to the DB within a Try/Catch. Return an empty list if it fails or 
    //a list of movies if it doesn't

    return moviesList;
}

@CacheEvict("moviesCache")
public void resetMoviesCache() {

}

Take the above methods used to cache and delete the cache variable moviesCache. My current logic invokes the getMovies method at start up, and it works fine. Then I refresh the moviesCache variable by evicting it and regenerating it by calling resetMoviesCache and getMovies methods in that order. That works fine as well.

Here's the issue. If a refresh occurs and the db operation fails causing the code to return an empty array, the cache is now empty. Is there a way to preserve the original cache that was stored before the failure and revert back to it when an empty array is returned? I look forward to learning from the responses. Thank you.

  • After evict the previous value is gone. You can request the database contents and put it into the cache, if successful. To put a value into the cache, access it directly via the `CacheManager`, or with `@CachePut` and an empty method body. – cruftex Oct 11 '20 at 12:55

0 Answers0