Skip to content

Commit

Permalink
te
Browse files Browse the repository at this point in the history
  • Loading branch information
정재욱 committed Mar 28, 2024
1 parent 66fb324 commit 2185fba
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ dependencies {
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'

// Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/ajou/hertz/common/config/ObjectMapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ajou.hertz.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

@Configuration
public class ObjectMapperConfig {

@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
objectMapper.registerModules(javaTimeModule, new Jdk8Module());
return objectMapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ajou.hertz.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.ajou.hertz.domain.user_auth_code.UserAuthCode;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
public class RedisTemplateConfig {

@Bean
public RedisTemplate<String, UserAuthCode> redisTemplate(
RedisConnectionFactory connectionFactory,
ObjectMapper objectMapper
) {
RedisTemplate<String, UserAuthCode> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(objectMapper, UserAuthCode.class));
return redisTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ajou.hertz.domain.user_auth_code;

import java.io.Serializable;
import java.time.LocalDateTime;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class UserAuthCode implements Serializable {

public static final int TTL_MINUTE = 5;

private String code;
private Long userId;

private LocalDateTime createdAt;

public UserAuthCode(String code, Long userId) {
this(code, userId, LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ajou.hertz.domain.user_auth_code.repository;

import com.ajou.hertz.domain.user_auth_code.UserAuthCode;

public interface UserAuthCodeRepository {

UserAuthCode save(UserAuthCode userAuthCode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ajou.hertz.domain.user_auth_code.repository;

import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.ajou.hertz.domain.user_auth_code.UserAuthCode;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Repository
public class UserAuthCodeRepositoryImpl implements UserAuthCodeRepository {

private final RedisTemplate<String, UserAuthCode> template;

@Override
public UserAuthCode save(UserAuthCode userAuthCode) {
template.opsForValue().set(
userAuthCode.getCode(),
userAuthCode,
UserAuthCode.TTL_MINUTE,
TimeUnit.MINUTES
);
return userAuthCode;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.data.redis.host=${REDIS_HOST}
spring.data.redis.port=${REDIS_PORT}

spring.servlet.multipart.max-request-size=70MB
spring.servlet.multipart.max-file-size=10MB

Expand Down

0 comments on commit 2185fba

Please sign in to comment.