Is there a way to accomplish this?
Say if the connection to redis is unavailable, assume cache does not exist and fetch it directly from database, instead of failing the entire app instead
@Cachable
public String cachingMethod(String cache) {
return "cache";
}
I was following this answer on SO: SO answer
But my spring boot still throws Exception on bootup and it does not fetch data from database:
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
...
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
...
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
...
Caused by: java.net.SocketTimeoutException: connect timed out
my configuration code is:
@Configuration
@EnableCaching
@EnableRedisHttpSession
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(128);
jedisPoolConfig.setMaxIdle(128);
jedisPoolConfig.setMinIdle(16);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
jedisPoolConfig.setTestWhileIdle(true);
jedisPoolConfig.setMinEvictableIdleTimeMillis(
Duration.ofSeconds(60).toMillis());
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(
Duration.ofSeconds(30).toMillis());
jedisPoolConfig.setNumTestsPerEvictionRun(3);
jedisPoolConfig.setBlockWhenExhausted(true);
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new
JedisConnectionFactory(jedisPoolConfig());
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(6379);
jedisConnectionFactory.afterPropertiesSet();
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
if (null == jedisConnectionFactory()) {
logger.error("Redis template service is not available");
return null;
};
template.setConnectionFactory(jedisConnectionFactory());
template.afterPropertiesSet();
return template;
}
@Bean
public RedisCacheManager redisCacheManager() {
Map<String, Long> expires = new HashMap<>();
expires.put("user:profile", 300L);
RedisCacheManager redisCacheManager = new
RedisCacheManager(redisTemplate());
redisCacheManager.setUsePrefix(true);
redisCacheManager.setDefaultExpiration(300);
redisCacheManager.setExpires(expires);
return redisCacheManager;
}
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName());
sb.append(method.getName());
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
}