DataBase/Redis
Spring Boot에서 Redis 사용하기
Drawhale
2023. 7. 24. 05:27
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
사용한 Dependency는 2가지 이다.
spring:
data:
redis:
host: 192.168.0.18
port: 6379
spring boot 2.x버전에서는 spring.redis.host와 spring.redis.port에 설정을 하지만 spring boot 3.x부터는 spring.data.redis에서 설정을 해야 한다.
@RestController
public class HelloController {
@Autowired
StringRedisTemplate stringRedisTemplate;
private final String fruit = "Fruit";
@GetMapping("/hello")
public String hello() {
return "hello";
}
@GetMapping("/setFruit")
public String setFruit(@RequestParam String name) {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
ops.set(fruit, name);
return "ok";
}
@GetMapping("/getFruit")
public String getFruit() {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
return ops.get(fruit);
}
}
간단한 RestController를 하나 만들어서 테스트 해볼 수 있다.
참고자료
https://www.baeldung.com/spring-data-redis-properties
반응형