Search code examples
javaspring-bootredisspring-datajedis

Springboot @Cacheable and @EnableRedisRepositories confict


I have reached a conflict situation between Spring @Repository and @Cacheable.

  1. I'm using @EnableCaching and have annotated a @Cacheable method. The expectation is that by default, it will create an in-memory cache without any additional configuration required.
@Service
public class ServiceA{

  @Cacheable("cacheName")
    public Response getResponse(){

    }
}
@EnableCaching
public class SpringBootApplication {
...
}
  1. I have @Repository class and a configuration class @EnableRedisRepositories which performs some CRUD operations towards a Redis DB reachable in a remote IP address that does not require any caching.
@Repository
public class RedisRepository{

    public void setSomething(){
    }

    public Something getSomething(){
    }
}
@Configuration
@EnableRedisRepositories
public class RedisConfig {
  @Bean
  public JedisConnectionFactory connectionFactory() {
  }
  @Bean
  public RedisTemplate<String, Object> redisTemplate() {
  }
}

The case is that with that configuration, the In-Memory cache is ignored and my cache is stored in the remote Redis DB.

How can I keep the in-memory cache and don't use remote Redis for caching?


Solution

  • I figured out this is not a bug but a feature. When spring detects spring-data-redis in the dependencies, it automatically enables distributed caching using redis by default. Thus, I had to set spring.cache.type=simple in application.properties

    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>