incognito nerd
incognito nerd

Reputation: 97

spring cache - how to revert back to original cache if update logic fails? - @cacheable @cacheput @cacheevict

Code:

@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?

Upvotes: 2

Views: 438

Answers (0)

Related Questions