-
Notifications
You must be signed in to change notification settings - Fork 3
[Resolves #6] redis config 설정 및 테스트 코드 #5
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
Merged
binimini
merged 9 commits into
KHUCapston-concoder:develop
from
JeongA-Shin:feature/1/snapshot-api
Nov 8, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
10ac9c2
chore : add config for redis
JeongA-Shin 739dc2a
chore: add config for redis
JeongA-Shin 183545c
test: add test for redis
JeongA-Shin 721a94a
feat: 엔티티 작성
binimini 6d52b93
feat: Repository 작성
binimini f36428b
refactor: 필요 없는 gitkeep 삭제
binimini 986e487
feat: 엔티티 작성
JeongA-Shin 5aab24e
refactor : 필요없는 주석 삭제
JeongA-Shin fdc8392
refactor : 콘솔 확인 삭제
JeongA-Shin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
package oncoding.concoder.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.lettuce.LettuceConnection; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.redis.port}") | ||
private int port; | ||
|
||
/* | ||
Spring Data Redis는 Redis에 두 가지 접근 방식을 제공합니다. | ||
하나는 RedisTemplate을 이용한 방식이며, 다른 하나는 RedisRepository를 이용한 방식 | ||
|
||
두 방식 모두 Redis에 접근하기 위해서는 Redis 저장소와 연결하는 과정이 필요 | ||
이 과정을 위해 RedisConnectionFactory 인터페이스(LettuceConnectionFactory )를 사용 | ||
* */ | ||
@Bean //redisConnectionFactory 이름의 빈(역할)은 LettuceConnectionFactory(host, port)를 구현체로 선택한 것 | ||
public LettuceConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
/* | ||
redisTemplate을 사용하여 Redis 저장소에 접근하기로 한다 | ||
RedisTemplate은 Redis 저장소에 오브젝트를 저장할 때 기본값으로 정의된 JdkSerializationRedisSerializer을 이용 | ||
*/ | ||
@Bean | ||
public RedisTemplate<String,Object> redisTemplate(){ | ||
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return redisTemplate; | ||
} | ||
|
||
/* | ||
대부분 레디스 key-value 는 문자열 위주이기 때문에 문자열에 특화된 템플릿을 제공 | ||
RedisTemplate 을 상속받은 클래스임. | ||
StringRedisSerializer로 직렬화함 | ||
*/ | ||
@Bean | ||
public StringRedisTemplate stringRedisTemplate(){ | ||
final StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); | ||
stringRedisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
stringRedisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
stringRedisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return stringRedisTemplate; | ||
} | ||
|
||
|
||
|
||
} |
Empty file.
This file contains hidden or 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,17 @@ | ||
package oncoding.concoder.model; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Category extends JpaBaseEntity { | ||
@Column | ||
@NotNull | ||
private String name; | ||
} |
This file contains hidden or 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,15 @@ | ||
package oncoding.concoder.model; | ||
|
||
import java.util.UUID; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
public class JpaBaseEntity { | ||
@Id | ||
@GeneratedValue | ||
private UUID id; | ||
} |
This file contains hidden or 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,17 @@ | ||
package oncoding.concoder.model; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Level extends JpaBaseEntity { | ||
@Column | ||
@NotNull | ||
private String name; | ||
} |
This file contains hidden or 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,41 @@ | ||
package oncoding.concoder.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.OneToMany; | ||
import javax.validation.constraints.NotNull; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Problem extends JpaBaseEntity { | ||
@Column(unique = true) | ||
@NotNull | ||
private Integer number; | ||
|
||
@Column | ||
@NotNull | ||
private String title; | ||
|
||
@Column | ||
private Float rate; | ||
|
||
@Column | ||
@NotNull | ||
private String content; | ||
|
||
@ManyToOne(fetch = FetchType.EAGER) | ||
private Level level; | ||
|
||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "problem") | ||
private List<ProblemCategory> categories = new ArrayList<>(); | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/oncoding/concoder/model/ProblemCategory.java
This file contains hidden or 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,18 @@ | ||
package oncoding.concoder.model; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.ManyToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ProblemCategory extends JpaBaseEntity { | ||
@ManyToOne | ||
private Problem problem; | ||
|
||
@ManyToOne | ||
private Category category; | ||
} |
This file contains hidden or 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,35 @@ | ||
package oncoding.concoder.model; | ||
|
||
import java.time.LocalDateTime; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.EntityListeners; | ||
import javax.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@EntityListeners(AuditingEntityListener.class) | ||
public class Snapshot extends JpaBaseEntity { | ||
|
||
@CreatedDate | ||
@Column(updatable=false, nullable = false) | ||
private LocalDateTime createdDate; | ||
|
||
@Column | ||
@NotNull | ||
private String memo; | ||
|
||
@Column | ||
@NotNull | ||
private String content; | ||
} |
Empty file.
9 changes: 9 additions & 0 deletions
9
src/main/java/oncoding/concoder/repository/CategoryRepository.java
This file contains hidden or 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 oncoding.concoder.repository; | ||
|
||
import java.util.UUID; | ||
import oncoding.concoder.model.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, UUID> { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/oncoding/concoder/repository/LevelRepository.java
This file contains hidden or 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 oncoding.concoder.repository; | ||
|
||
import java.util.UUID; | ||
import oncoding.concoder.model.Level; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface LevelRepository extends JpaRepository<Level, UUID> { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/oncoding/concoder/repository/ProblemCategoryRepository.java
This file contains hidden or 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 oncoding.concoder.repository; | ||
|
||
import java.util.UUID; | ||
import oncoding.concoder.model.ProblemCategory; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ProblemCategoryRepository extends JpaRepository<ProblemCategory, UUID> { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/oncoding/concoder/repository/ProblemRepository.java
This file contains hidden or 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 oncoding.concoder.repository; | ||
|
||
import java.util.UUID; | ||
import oncoding.concoder.model.Problem; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ProblemRepository extends JpaRepository<Problem, UUID> { | ||
|
||
} |
This file contains hidden or 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 +1,4 @@ | ||
|
||
spring: | ||
redis: | ||
host: localhost | ||
port: 6379 |
88 changes: 88 additions & 0 deletions
88
src/test/java/oncoding/concoder/config/RedisBasicTest.java
This file contains hidden or 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,88 @@ | ||
package oncoding.concoder.config; | ||
|
||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
|
||
@SpringBootTest | ||
@AutoConfigureTestDatabase(replace = Replace.NONE) | ||
public class RedisBasicTest { | ||
|
||
@Autowired | ||
RedisTemplate<String, String> redisTemplate; | ||
|
||
|
||
public static class RedisUserDto{ | ||
private String name; | ||
private String password; | ||
|
||
public RedisUserDto(String name, String password){ | ||
this.name = name; | ||
this.password = password; | ||
} | ||
|
||
public RedisUserDto() { | ||
|
||
} | ||
|
||
public String getName(){ | ||
return this.name; | ||
} | ||
|
||
public String getPassword(){ | ||
return this.password; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 단순 연결 테스트 - key와 value 삽입 | ||
*/ | ||
@Test | ||
void redisConnectionTest() { | ||
final String key = "a"; | ||
final String data = "1"; | ||
|
||
final ValueOperations<String, String> valueOperations = redisTemplate.opsForValue(); | ||
valueOperations.set(key, data); | ||
|
||
final String s = valueOperations.get(key); | ||
Assertions.assertThat(s).isEqualTo(data); | ||
} | ||
|
||
@Test | ||
void redisInsertObject(){ | ||
RedisUserDto redisUserDto = new RedisUserDto("kenux", "password"); | ||
|
||
final ValueOperations<String, String> valueOperations = redisTemplate.opsForValue(); | ||
valueOperations.set(redisUserDto.getName(), String.valueOf(redisUserDto)); | ||
|
||
final String result = valueOperations.get(redisUserDto.getName()); | ||
|
||
} | ||
|
||
/** | ||
* 위 코드 redis에 삽입한 아이템에 대해서 5초의 expire time을 설정하고, 5초가 지난 후에 redis에서 해당 키가 조회되는지 테스트하는 코드 | ||
* @throws InterruptedException | ||
*/ | ||
@Test | ||
void redisExpireTest() throws InterruptedException { | ||
final String key = "a"; | ||
final String data = "1"; | ||
|
||
final ValueOperations<String, String> valueOperations = redisTemplate.opsForValue(); | ||
valueOperations.set(key, data); | ||
final Boolean expire = redisTemplate.expire(key, 5, TimeUnit.SECONDS); | ||
Thread.sleep(6000); | ||
final String s = valueOperations.get(key); | ||
|
||
} | ||
|
||
} |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
요런 주석은 코드에 남기기보다는 PR이나 노션에 남겨주면 좋을 듯 🙃
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.
스냅샷 이슈 만들어서 그거 닫는다는 게 그냥 스냅샷 구현 끝내고 한 번에 pr 올리라는 거,,,? 제목은 곧 수정하겠슴당
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.
아 스냅샷 이슈가 아니라 레디스 환경설정 이슈 잘못 말했어용
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.
ㅇㅎㅇㅎ,, 근데 그러면 지금 이슈를 하나 파야 하나,,,?