mercredi 5 août 2015

Dsiable Cache Abstraction from Code Based upon Some Configuration parameters


I am using Redis cache abstraction in my Spring boot application. Going through many things i have come to one requirement , I want to make Caching enable or disable based upon some configuration. following is the code where i use cache:

@Override
@Cacheable(value = IC_CACHE, key = "#id")
public IssueCategory getIssueCategoriesById(Integer id) {
    return issueCategoriesRepo.findById(id);
}

And here is how i configure it:

  @Bean
public RedisConnectionFactory redisConnectionFactory(@Value("${redis.host}") String redisHost,
        @Value("${redis.port}") Integer redisPort) {
    JedisConnectionFactory cf = new JedisConnectionFactory();
    cf.setHostName(redisHost);
    cf.setPort(redisPort);
   return cf;
}

@Bean(name = "redisTemplate")
RedisTemplate<Object,Object> redisTemplate() {
    final RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>();
    template.setConnectionFactory(applicationContext.getBean(RedisConnectionFactory.class));
    return template;
}

@Bean
public CacheManager cacheManager() {
    RedisCacheManager redisCacheManager = new PieRedisCacheManager(
            (RedisTemplate<?, ?>) applicationContext.getBean("redisTemplate"));
    redisCacheManager.setUsePrefix(true);
    return redisCacheManager;
}

Say I have a variable in properties file which says :

rediscache=disable

Is there a way to disable cache at all based upon that configuration??

so that In the first code where i use @cacheable, code should directly get data from database

Please Advice.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire