Spring Boot Config

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

application.yml

spring:
	redis:
		host: localhost
		port: 6379

	cache:
		type: redis
    redis:
      time-to-live: 600000  # in milliseconds (10 minutes)

RedisConfig.java (@Configuration)

@Configuration
@EnableCaching
public class RedisConfig {

	@Bean
	public RedisTemplate<String, Object> redisTemplate (RedisConnectionFactory connectionFactory) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

				return template;
 	}
}

RedisService.java (RedisTemplate)

@Service
public class RedisService {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	public void save (String key, Object value, long ttlSeconds) {
	  redisTemplate.opsForValue()
	               .set(key, value, Duration.ofSeconds(ttlSeconds));
  }
	
	public Object get (String key) {
		return redisTemplate.opsForValue().get(key);
  }
	
	public void delete (String key) {
    redisTemplate.delete(key);
  }
}

Redis as CACHE

UserService.java