Skip to content

Commit 68905d3

Browse files
authored
refactor : 문제 카테고리 read 시 캐쉬 설정 (#112)
1 parent 7faa72d commit 68905d3

File tree

9 files changed

+21
-13
lines changed

9 files changed

+21
-13
lines changed

src/main/java/org/ezcode/codetest/application/submission/model/SubmissionContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public Problem getProblem() {
128128
}
129129

130130
public List<String> getCategories() {
131-
return getProblem().getCategories().stream().map(ProblemCategory::getCategoryForKor).toList();
131+
return problemInfo.categories();
132132
}
133133

134134
public void incrementTotalSubmissions() {

src/main/java/org/ezcode/codetest/domain/game/model/character/CharacterRealStat.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public void increase(Stat stat, double rate) {
9696
case DEBUGGING:
9797
this.crit += rate / 10;
9898
this.def += rate / 10;
99-
this.atk += rate / 10;
10099
this.stun += rate / 10;
101100
break;
102101
case OPTIMIZATION:

src/main/java/org/ezcode/codetest/domain/problem/model/ProblemInfo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import java.util.List;
44

5-
import org.ezcode.codetest.domain.problem.model.entity.Category;
65
import org.ezcode.codetest.domain.problem.model.entity.Problem;
76
import org.ezcode.codetest.domain.problem.model.entity.Testcase;
87

98
public record ProblemInfo(
109

1110
Problem problem,
1211

13-
List<Testcase> testcaseList
12+
List<Testcase> testcaseList,
13+
14+
List<String> categories
1415

1516
) {
1617
public long getTimeLimit() {
@@ -24,4 +25,5 @@ public long getMemoryLimit() {
2425
public int getTestcaseCount() {
2526
return this.testcaseList.size();
2627
}
28+
2729
}

src/main/java/org/ezcode/codetest/domain/problem/model/entity/Problem.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.ezcode.codetest.domain.problem.model.entity;
22

33
import java.util.ArrayList;
4-
import java.util.LinkedHashSet;
54
import java.util.List;
6-
import java.util.Set;
75

86
import org.ezcode.codetest.common.base.entity.BaseEntity;
97
import org.ezcode.codetest.domain.problem.model.enums.Difficulty;
@@ -76,9 +74,6 @@ public class Problem extends BaseEntity {
7674
@OneToMany(mappedBy = "problem", cascade = CascadeType.ALL, orphanRemoval = true)
7775
private List<Testcase> testcases = new ArrayList<>();
7876

79-
@OneToMany(mappedBy = "problem", cascade = CascadeType.ALL, orphanRemoval = true)
80-
private Set<ProblemCategory> categories = new LinkedHashSet<>();
81-
8277
@ElementCollection(fetch = FetchType.LAZY)
8378
private List<String> imageUrl = new ArrayList<>();
8479

src/main/java/org/ezcode/codetest/domain/problem/model/entity/ProblemCategory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ public static ProblemCategory from(Problem problem, Category category) {
5757
public String getCategoryForKor() {
5858
return category.getKorName();
5959
}
60+
6061
}

src/main/java/org/ezcode/codetest/domain/problem/service/ProblemDomainService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ public ProblemInfo getProblemInfo(Long problemId) {
114114
Problem problem = problemRepository.findProblemWithTestcasesById(problemId)
115115
.orElseThrow(() -> new ProblemException(ProblemExceptionCode.PROBLEM_NOT_FOUND));
116116

117-
return new ProblemInfo(problem, problem.getTestcases());
117+
List<ProblemCategory> categories = problemCategoryRepository.findByProblemId(problemId);
118+
119+
List<String> categoryNames = categories.stream().map(cat -> cat.getCategory().getKorName()).toList();
120+
121+
return new ProblemInfo(problem, problem.getTestcases(), categoryNames);
118122
}
119123

120124
}

src/main/java/org/ezcode/codetest/infrastructure/cache/config/CaffeineCacheConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class CaffeineCacheConfig {
1818

1919
@Bean
2020
public CacheManager cacheManager() {
21+
2122
CaffeineCache countCache = new CaffeineCache("counts",
2223
Caffeine.newBuilder()
2324
.expireAfterWrite(Duration.ofMinutes(1))
@@ -53,10 +54,16 @@ public CacheManager cacheManager() {
5354
.expireAfterWrite(Duration.ofMinutes(10))
5455
.build());
5556

57+
CaffeineCache problemCategory = new CaffeineCache("problemCategory",
58+
Caffeine.newBuilder()
59+
.expireAfterWrite(Duration.ofMinutes(10))
60+
.build());
61+
5662
SimpleCacheManager manager = new SimpleCacheManager();
5763
manager.setCaches(
5864
List.of(skillCache, itemsByCategoryCache, encountersCache, countCache, historyCache, choiceCache,
59-
categoryStatCache));
65+
categoryStatCache, problemCategory));
66+
6067
return manager;
6168
}
6269
}

src/main/java/org/ezcode/codetest/infrastructure/persistence/repository/problem/ProblemCategoryRepositoryImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.ezcode.codetest.domain.problem.model.entity.ProblemCategory;
66
import org.ezcode.codetest.domain.problem.repository.ProblemCategoryRepository;
7+
import org.springframework.cache.annotation.Cacheable;
78
import org.springframework.stereotype.Repository;
89

910
import lombok.RequiredArgsConstructor;
@@ -40,6 +41,7 @@ public void deleteAllByProblemId(Long problemId) {
4041
}
4142

4243
@Override
44+
@Cacheable(value = "problemCategory", key = "#problemId")
4345
public List<ProblemCategory> findByProblemId(Long problemId) {
4446

4547
return problemCategoryRepository.findByProblemId(problemId);

src/main/java/org/ezcode/codetest/infrastructure/persistence/repository/problem/ProblemJpaRepository.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public interface ProblemJpaRepository extends JpaRepository<Problem, Long> {
1616
select distinct p
1717
from Problem p
1818
left join fetch p.testcases
19-
left join fetch p.categories c
20-
left join fetch c.category
2119
where p.id = :problemId
2220
""")
2321
Optional<Problem> findProblemWithTestcasesById(@Param("problemId") Long problemId);

0 commit comments

Comments
 (0)