-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat-be: Redis 로컬 환경 설정 #822
base: be/develop
Are you sure you want to change the base?
Changes from all commits
e34172a
9cecb57
7ae3119
8e7778f
64fd840
3f43993
1b50e53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: '3.8' | ||
|
||
services: | ||
redis: | ||
image: redis:latest | ||
container_name: redis-local | ||
ports: | ||
- "6379:6379" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 6379-> 6379로 포트 바인딩을 같은 포트 번호로 하는 것보다 로컬에서 모종의 의유로 중복 사용될 수도 있을 것 같아서요 |
||
networks: | ||
- redis_network | ||
|
||
networks: | ||
redis_network: | ||
driver: bridge |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test용이라면 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.cruru; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/redis-test") | ||
public class RedisTestController { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 테스트 클래스에서 key의 life cycle을 관리하는 테스트도 만들어보시는 것이 어떨까요? 저희 메일 인증 기능의 핵심인 것 같아서요! |
||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
|
||
public RedisTestController(RedisTemplate<String, String> redisTemplate) { | ||
this.redisTemplate = redisTemplate; | ||
} | ||
|
||
// curl -X POST "http://localhost:8080/redis-test?key=testKey&value=testValue" | ||
@PostMapping | ||
public ResponseEntity<String> setKey(@RequestParam String key, @RequestParam String value) { | ||
redisTemplate.opsForValue().set(key, value); | ||
return ResponseEntity.ok("key-value: " + key + " = " + value); | ||
} | ||
|
||
// http://localhost:8080/redis-test?key=testKey | ||
@GetMapping | ||
public ResponseEntity<String> getKey(@RequestParam String key) { | ||
String value = redisTemplate.opsForValue().get(key); | ||
if (value == null) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("key " + key + " not found"); | ||
} | ||
return ResponseEntity.ok("value for key " + key + " is: " + value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.cruru.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Profile({"local", "test"}) | ||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int port; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { | ||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(connectionFactory); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.afterPropertiesSet(); | ||
return redisTemplate; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.cruru; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
@ActiveProfiles("test") | ||
@SpringBootTest | ||
class RedisTest { | ||
|
||
@Autowired | ||
private RedisTemplate<String, String> redisTemplate; | ||
|
||
@Test | ||
public void shouldConnectToRedis() { | ||
Boolean isAvailable = redisTemplate.getConnectionFactory() | ||
.getConnection() | ||
.ping() | ||
.equalsIgnoreCase("PONG"); | ||
assertThat(isAvailable).isTrue(); | ||
} | ||
|
||
@Test | ||
public void setAndGet() { | ||
// given | ||
String key = "testKey"; | ||
String value = "testValue"; | ||
|
||
// when | ||
redisTemplate.opsForValue().set(key, value); | ||
String actual = redisTemplate.opsForValue().get(key); | ||
|
||
// then | ||
assertThat(actual).isEqualTo(value); | ||
redisTemplate.delete("testKey"); | ||
} | ||
|
||
@Test | ||
public void delete() { | ||
// given | ||
String key = "testDeleteKey"; | ||
String value = "testValue"; | ||
redisTemplate.opsForValue().set(key, value); | ||
|
||
// when | ||
redisTemplate.delete(key); | ||
String actual = redisTemplate.opsForValue().get(key); | ||
|
||
// then | ||
assertThat(actual).isNull(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제목이 local인 것에 비해 redis만 관리하고 있는 스크립트니까 redis로 파일명을 바꾸거나 따로 local용 docker-compose 파일을 만드시는게 어떨까요?