Question: How to Refresh Redis Cache in Spring Boot?


To refresh the Redis cache in a Spring Boot application, you can use the CacheManager interface provided by Spring. This interface allows you to manage and manipulate caches programmatically, including refreshing cache entries.

Here's a guide on how to refresh Redis cache in Spring Boot:

  1. Configure cache manager in your Spring Boot application: In your configuration class, create a method annotated with @Bean that returns a RedisCacheManager instance.
public class CacheConfiguration {

    private RedisConnectionFactory redisConnectionFactory;

    public CacheManager cacheManager() {
        RedisCacheConfiguration cacheConfiguration = 
                                   .entryTtl(Duration.ofMinutes(10)); // Set time to live

        return RedisCacheManager.builder(redisConnectionFactory)
  1. Programmatically refresh the cache using CacheManager: Inject the CacheManager bean into your service or component class and use it to clear and reload cache entries.
public class YourService {

    private CacheManager cacheManager;

    // A method to refresh your cache
    public void refreshCache(String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null) {
            cache.clear(); // Clearing the cache

    // Other methods and code for your service
  1. Call the refreshCache method from your controller or another class to refresh the cache:
public class YourController {

    private YourService yourService;

    public ResponseEntity<String> refreshCache(@PathVariable String cacheName) {
        return ResponseEntity.ok("Cache " + cacheName + " has been refreshed.");

    // Other methods and code for your controller

Now, you can call the /api/refresh-cache/{cacheName} endpoint to refresh the specific Redis cache in your Spring Boot application.

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.