1. Redis 개발 실습 구성하기


<aside> 1️⃣ 프로젝트 구성

</aside>

<aside> 2️⃣ Redis 라이브러리 사용

</aside>

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-data-redis'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Untitled

<aside> 3️⃣ controller 코드 작성

</aside>

@RestController
@RequiredArgsConstructor
public class HelloController {

    private final StringRedisTemplate redisTemplate;

    @GetMapping("/hello")
    public String hello() {
        return "hello world";
    }
    
    @GetMapping("/getFruit")
    public String getFruit() {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String fruitName = ops.get("fruit");

        return fruitName;
    }

    @GetMapping("/setFruit")
    public String setFruit(@RequestParam String name) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set("fruit", name);

        return "saved.";
    }

}
spring:
  redis:
    host: localhost
    port: 6379