diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 4f270e0a..8f244e32 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -22,6 +22,19 @@ jobs: distribution: 'corretto' java-version: '17' + - name: Setup MySQL + uses: samin/mysql-action@v1 + with: + character set server: 'utf8' + mysql database: 'moddy' + mysql user: 'moddy' + mysql password: ${{ secrets.DatabasePassword }} + + - name: Start Redis + uses: supercharge/redis-github-action@1.7.0 + with: + redis-version: 6 + - name: application.yml 생성 run: | cd src/main/resources diff --git a/build.gradle b/build.gradle index 1f483a7c..f2f7358a 100644 --- a/build.gradle +++ b/build.gradle @@ -47,6 +47,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-aop' + implementation 'org.springframework.boot:spring-boot-starter-data-redis' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/com/moddy/server/config/redis/RedisConfig.java b/src/main/java/com/moddy/server/config/redis/RedisConfig.java new file mode 100644 index 00000000..87c3c623 --- /dev/null +++ b/src/main/java/com/moddy/server/config/redis/RedisConfig.java @@ -0,0 +1,31 @@ +package com.moddy.server.config.redis; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +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.repository.configuration.EnableRedisRepositories; + +@Configuration +@EnableRedisRepositories +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 redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory()); + return redisTemplate; + } +} diff --git a/src/main/java/com/moddy/server/config/redis/VerificationCodeRedisService.java b/src/main/java/com/moddy/server/config/redis/VerificationCodeRedisService.java new file mode 100644 index 00000000..0960a977 --- /dev/null +++ b/src/main/java/com/moddy/server/config/redis/VerificationCodeRedisService.java @@ -0,0 +1,35 @@ +package com.moddy.server.config.redis; + +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +@Component +@RequiredArgsConstructor +public class VerificationCodeRedisService { + private final RedisTemplate redisTemplate; + private static final int EXPIRE_VERIFICATION_CODE_TIME = 3; + + public void saveVerificationCode(String phoneNumber, String verificationCode) { + ValueOperations values = redisTemplate.opsForValue(); + values.set(phoneNumber, verificationCode); + redisTemplate.expire(phoneNumber, EXPIRE_VERIFICATION_CODE_TIME, TimeUnit.MINUTES); + } + + public boolean isVerificationCodeEmpty(String phoneNumber) { + ValueOperations values = redisTemplate.opsForValue(); + return values.get(phoneNumber) == null; + } + + public String getVerificationCode(String phoneNumber) { + ValueOperations values = redisTemplate.opsForValue(); + return values.get(phoneNumber); + } + + public void deleteVerificationCode(String phoneNumber) { + redisTemplate.delete(phoneNumber); + } +} diff --git a/src/test/java/com/moddy/server/config/redis/VerificationCodeRedisServiceTest.java b/src/test/java/com/moddy/server/config/redis/VerificationCodeRedisServiceTest.java new file mode 100644 index 00000000..b5a34a50 --- /dev/null +++ b/src/test/java/com/moddy/server/config/redis/VerificationCodeRedisServiceTest.java @@ -0,0 +1,38 @@ +package com.moddy.server.config.redis; + +//@SpringBootTest +class VerificationCodeRedisServiceTest { +// @Autowired +// private VerificationCodeRedisService verificationCodeRedisService; +// +// @Test +// @DisplayName("전화 번호를 키로 사용하여 인증 코드를 저장할 수 있다.") +// public void saveVerificationCodeTest() { +// // given +// String phoneNumber = "01000000000"; +// String verificationCode = "123456"; +// +// // when +// verificationCodeRedisService.saveVerificationCode(phoneNumber, verificationCode); +// +// // then +// String response = verificationCodeRedisService.getVerificationCode(phoneNumber); +// assertThat(response).isEqualTo(verificationCode); +// } +// +// @Test +// @DisplayName("특정 전화 번호의 값을 제거할 수 있다.") +// public void deleteVerificationCodeTest() { +// // given +// String phoneNumber = "01000000000"; +// String verificationCode = "123456"; +// verificationCodeRedisService.saveVerificationCode(phoneNumber, verificationCode); +// +// // when +// verificationCodeRedisService.deleteVerificationCode(phoneNumber); +// +// // then +// boolean response = verificationCodeRedisService.isVerificationCodeEmpty(phoneNumber); +// assertThat(response).isTrue(); +// } +} \ No newline at end of file