RediscacheManager instance with Spring data redis version above 2.0x

63
August 13, 2019, at 10:30 PM

I was using Spring data Redis version 1.4.4 earlier. Since I had to configure Read timeout also in addition to connection timeout, I've upgraded the version to 2.1.6. But now I'm unable to create Rediscachemanager instance with RedisTemplate since it's deprecated from spring data redis version 2.0. Can someone please help with a way to achieve below similar config (Key/Value serialization with RedisTemplate) with upgraded version.

final JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redisHost);      
redisConnectionFactory.setTimeout(10000);
redisConnectionFactory.setPort(port);
redisConnectionFactory.setUsePool(true);
redisConnectionFactory.afterPropertiesSet();
@Bean(name = "redisCacheManager")
    public CacheManager cacheManager(final RedisTemplate<String,Object> redisTemplate) {
        final RedisCacheManager manager = new RedisCacheManager(redisTemplate());
        manager.setDefaultExpiration(Long.parseLong(expiryInSecs));
        return manager;
    }
    @Bean(name="redisTemplate")
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setKeySerializer(stringRedisSerializer());
        redisTemplate.setHashKeySerializer(stringRedisSerializer());
        redisTemplate.setValueSerializer(stringRedisSerializer());
        redisTemplate.setHashValueSerializer(stringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
Answer 1

If you include Spring Data Redis and Lettuce as the driver (which I'd recommend), here's the configuration you'd need:

  @Bean
  public RedisConnectionFactory connectionFactory() {
    LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
        .commandTimeout(Duration.ofSeconds(2))
        .shutdownTimeout(Duration.ZERO)
        .build();
    return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379), clientConfig);
  }
  @Bean(name = "redisCacheManager")
  public CacheManager cacheManager(final RedisConnectionFactory factory) {
    Duration expiration = Duration.ofSeconds(12345);
    RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
        .builder(factory)
        .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(expiration));

    return builder.build();
  }
  @Bean(name="redisTemplate")
  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(factory);
    redisTemplate.setKeySerializer(stringRedisSerializer());
    redisTemplate.setHashKeySerializer(stringRedisSerializer());
    redisTemplate.setValueSerializer(stringRedisSerializer());
    redisTemplate.setHashValueSerializer(stringRedisSerializer());
    return redisTemplate;
  }
READ ALSO
JavaFX : StackPane Sequential Transition

JavaFX : StackPane Sequential Transition

I am trying to switch between 3 different AnchorPane (on click of a Button) with a FadeTransition and below is my code,

33
How can I convert this code to prepared-statement or statement in jdbc? [on hold]

How can I convert this code to prepared-statement or statement in jdbc? [on hold]

I'm trying to insert this values to a new table but eclipse doesn't read this query:

69
Taking multiple lines with multiple inputs in each Lines

Taking multiple lines with multiple inputs in each Lines

My requirement is to take multiple inputs from user on multiple lines

52
BouncyCastle get specific crypto algorithm classes to strip down size

BouncyCastle get specific crypto algorithm classes to strip down size

I'm creating a library that requires ECC (elliptic curve cryptography, secp256k1) algorithmAs of now, I'm using BouncyCastle and it works perfectly

75