-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
250 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/hackathonteam1/refreshrator/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.hackathonteam1.refreshrator.config; | ||
|
||
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.RedisConnection; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisKeyValueAdapter; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
private final String redisHost; | ||
private final int redisPort; | ||
|
||
public RedisConfig(@Value("${spring.data.redis.host}") String redisHost, | ||
@Value("${spring.data.redis.port}")int redisPort){ | ||
this.redisHost = redisHost; | ||
this.redisPort = redisPort; | ||
} | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory(){ | ||
return new LettuceConnectionFactory(redisHost, redisPort); | ||
} | ||
|
||
@Bean | ||
public <K, V> RedisTemplate<K, V> redisTemplate(RedisConnectionFactory redisConnectionFactory){ | ||
RedisTemplate<K, V> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); //key를 string으로 시리얼라이즈 | ||
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //value를 json으로 시리얼라이즈 | ||
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //Hash의 key를 String으로 시리얼라이즈 | ||
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); //Hash의 value를 json으로 시리얼라이즈 | ||
return redisTemplate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/com/hackathonteam1/refreshrator/dto/response/auth/TokenResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package com.hackathonteam1.refreshrator.dto.response.auth; | ||
|
||
import com.hackathonteam1.refreshrator.entity.RefreshToken; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class TokenResponseDto { | ||
private String AccessToken; | ||
private RefreshToken refreshToken; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/hackathonteam1/refreshrator/entity/RefreshToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.hackathonteam1.refreshrator.entity; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.index.Indexed; | ||
|
||
import java.util.UUID; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class RefreshToken { | ||
@Id | ||
private UUID tokenId; | ||
@Indexed | ||
private UUID userId; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/hackathonteam1/refreshrator/exception/RedisException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.hackathonteam1.refreshrator.exception; | ||
|
||
import com.hackathonteam1.refreshrator.exception.errorcode.ErrorCode; | ||
|
||
public class RedisException extends CustomException{ | ||
public RedisException(ErrorCode errorCode, String detail) { | ||
super(errorCode, detail); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/hackathonteam1/refreshrator/util/RedisUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.hackathonteam1.refreshrator.util; | ||
|
||
|
||
import com.hackathonteam1.refreshrator.exception.RedisException; | ||
import com.hackathonteam1.refreshrator.exception.errorcode.ErrorCode; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class RedisUtil<K, V> { | ||
private final RedisTemplate<K, V> redisTemplate; | ||
|
||
public void save(K key, V value, long timeout, TimeUnit timeUnit) { | ||
try { | ||
redisTemplate.opsForValue().set(key, value, timeout, timeUnit); | ||
} catch (Exception e) { | ||
throw new RedisException( ErrorCode.REDIS_ERROR, e.getMessage()); | ||
} | ||
} | ||
|
||
public void delete(K key) { | ||
try { | ||
redisTemplate.delete(key); | ||
}catch (Exception e){ | ||
throw new RedisException( ErrorCode.REDIS_ERROR, e.getMessage()); | ||
} | ||
} | ||
|
||
public Optional<V> findById(K key){ | ||
V result = redisTemplate.opsForValue().get(key); | ||
return Optional.ofNullable(result); | ||
} | ||
} |