title: how to configure redis ttl with spring boot 2.0
body: <p>I am using redis in spring boot 2.0. </p>
  
  <p>I wanted the data stored in redis to be set to TTL. </p>
  
  <p>So I searched to find a way, and I wrote the following example code.</p>
  
  <p>Person.java</p>
  
  <pre><code>@Data
  @Builder
  @RedisHash("Person")
  public class Person implements Serializable {
  
      @Id
      private Long id;
  
      private PersonInfo info;
  }
  </code></pre>
  
  <p>PersonInfo.java</p>
  
  <pre><code>@Data
  @Builder
  @AllArgsConstructor
  @NoArgsConstructor
  public class PersonInfo implements Serializable {
  
      private String name;
  
      private Long age;
  }
  </code></pre>
  
  <p>RedisConfig.java</p>
  
  <pre><code>@Configuration
  @Slf4j
  public class RedisConfig extends CachingConfigurerSupport {
  
      @Value("${spring.redis.host}")
      private String redisHost;
  
      @Value("${spring.redis.port}")
      private int redisPort;
  
      @Value("${spring.redis.expiretime}")
      private Long expireTime;
  
  
  
      @Bean
      public LettuceConnectionFactory lettuceConnectionFactory() {
  
          log.info("Info -&gt; Lettuce Connection Factory");
  
          LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
          lettuceConnectionFactory.setHostName(this.redisHost);
          lettuceConnectionFactory.setPort(this.redisPort);
  
          log.info("RedisHost  -&gt; " + lettuceConnectionFactory.getHostName());
          log.info("RedisPort  -&gt; " + lettuceConnectionFactory.getPort());
          log.info("ExpireTime -&gt; " + expireTime);
  
          return lettuceConnectionFactory;
      }
  
  
       @Bean
      public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
  
          log.info("Info -&gt; Redis Template");
  
          RedisTemplate redisTemplate = new RedisTemplate();
  
          redisTemplate.setKeySerializer(new StringRedisSerializer());
  
  
          redisTemplate.setConnectionFactory(lettuceConnectionFactory);
          return redisTemplate;
      }
  
  
  
      @Bean
      public RedisCacheConfiguration cacheConfiguration() {
  
          log.info("Info -&gt; Redis Cache Configuration");
  
          RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig()
                  .entryTtl(Duration.ofSeconds(expireTime))
                  .disableCachingNullValues()
                  .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
          cacheConfig.usePrefix();
  
          log.info("Duration -&gt; " + cacheConfig.getTtl().getSeconds());
  
          return cacheConfig;
      }
  
  
      @Bean
      public RedisCacheManager cacheManager() {
  
          log.info("Info -&gt; Redis Cache Manager");
  
          RedisCacheManager rcm = RedisCacheManager
                  .builder(this.lettuceConnectionFactory())
                  .cacheDefaults(this.cacheConfiguration())
                  .build();
  
          return rcm;
      }
  }
  </code></pre>
  
  <p>PersonRedisRepository</p>
  
  <pre><code>public interface PersonRedisRepository extends CrudRepository&lt;Person, Long&gt; {}
  </code></pre>
  
  <p>PersonController.java</p>
  
  <pre><code>@RestController
  @Slf4j
  public class PersonController {
  
      @Autowired
      private PersonRedisRepository personRedisRepository;
  
  
  
      @PostMapping("/person/{id}")
      public Person createPerson(@PathVariable("id") Long id) {
  
          log.info("Info -&gt; CreatePerson START ================= ");
  
          Person person = Person.builder()
                  .id(id)
                  .info(PersonInfo.builder()
                          .name("Test -&gt; " + id)
                          .age(id + 15)
                          .build()
                  )
                  .build();
  
          personRedisRepository.save(person);
  
          Person getPerson = personRedisRepository.findById(id).get();
          log.info("Info -&gt; getPerson : " + getPerson.toString());
  
          log.info("Info -&gt; createPerson END   ================= ");
  
          return getPerson;
      }
  
  
  
      @GetMapping("/person/{id}")
      public Person findPerson(@PathVariable("id") Long id) {
  
          log.info("Info -&gt; FindPerson START ================= ");
  
          Person findedPerson = personRedisRepository.findById(id).get();
          log.info("Info -&gt; findedPerson : " + findedPerson.toString());
  
          log.info("Info -&gt; FindPerson END   ================= ");
  
         // return findedPerson;
          return null;
      }
  }
  </code></pre>
  
  <p>application.yml</p>
  
  <pre><code>spring:
    # Docker Redis
    redis:
      host: 192.168.99.100
      port: 6379
      expiretime: 100
  
  server:
    port: 8083
  </code></pre>
  
  <p>I wrote RedisConfig.java and others code and tested it as below.</p>
  
  <p>I'm used Rest Client to request </p>
  
  <p>Request</p>
  
  <pre><code>GET http://localhost:8083/person/7 HTTP/1.1
  </code></pre>
  
  <pre><code>redis:6379&gt; keys *
  1) "Person"
  2) "Person:3"
  redis:6379&gt; hgetall Person:3
  1) "_class"
  2) "my.cachemanager.redis.person.domain.Person"
  3) "id"
  4) "3"
  5) "info.name"
  6) "Test -&gt; 3"
  7) "info.age"
  8) "18"
  redis:6379&gt; TTL Person:3
  (integer) -1
  </code></pre>
  
  <p>I confirmed that the data was stored in redis via redis-cli.
  However, I also confirmed that the ttl time was not applied.</p>
  
  <p>How can I apply the TTL to the data stored in Redis? </p>
  
  <p>I would really appreciate your help.</p>