Skip to content

Commit

Permalink
Merge pull request #261 from SWM-NM/feat/#260
Browse files Browse the repository at this point in the history
✏️ [FIX] Redis 키 attemptProblemId -> test 문제 번호 #260
  • Loading branch information
miiiinju1 authored Sep 12, 2023
2 parents b1d794b + bbe99d9 commit cf826a1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public interface AttemptProblemRepository extends JpaRepository<AttemptProblem, Long> {
List<AttemptProblem> findAllByMember_MemberId(Long memberId);
List<AttemptProblem> findAllByTest_TestId(Long testId);
List<AttemptProblem> findAllByTest(Test test);
List<AttemptProblem> findAllByTestOrderByAttemptProblemIdAsc(Test test);
List<AttemptProblem> findAttemptProblemsByTest_TestId(Long testId);

//List<AttemptProblem> findAttemptProblemsByTest_TestIdOrderByAttemptProblemIdAsc(Long testId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public void saveTempCode(@RequestBody TempCodeDto tempCodeDto) {
@Operation(summary = "저장된 코드를 확인합니다", description = "테스트 중일 때, 문제 번호를 바꿀 때 코드 정보를\n" +
"testId와 attemptProblemId를 이용하여 가져온다. \n")
public ResponseEntity<TempCode> getTempCode(@RequestParam String testId,
@RequestParam String attemptProblemId){
@RequestParam String problemNumber){

String key =String.format("tests:%s:problems:%s",testId, attemptProblemId);
String key =String.format("testId:%s:problemNumber:%s",testId, problemNumber);

return new ResponseEntity<>(tempCodeService.getTempCode(key), HttpStatus.OK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
@NoArgsConstructor
public class TempCodeDto {
public String testId;
public String attemptProblemId;
public String problemNumber;
public String code;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Service
@RequiredArgsConstructor
Expand All @@ -27,11 +28,13 @@ public class TempCodeService {
// key를 생성 후, 시작 시점에 tempCode를 저장하여 TTL을 설정한다
// 그래서 시험이 끝나는 시간을 보장할 수 있음
public void initTempCodeCacheWhenTestStart(Test test){
List<AttemptProblem> attemptProblems = attemptProblemRepository.findAllByTest(test);
List<AttemptProblem> attemptProblems = attemptProblemRepository.findAllByTestOrderByAttemptProblemIdAsc(test);
LocalDateTime now = LocalDateTime.now();
AtomicInteger i = new AtomicInteger(1);

attemptProblems.forEach(attemptProblem-> {
String key = generateKey(test, attemptProblem);
String key = generateKey(test, i.getAndIncrement());

//끝나는 시간
LocalDateTime endTime = now.plusMinutes(test.getTestTime());
Duration duration = Duration.between(now, endTime);
Expand Down Expand Up @@ -76,10 +79,10 @@ public TempCode getTempCode(String key) {
);
}

public String generateKey(Test test, AttemptProblem attemptProblem) {
return String.format("tests:%s:problems:%s",test.getTestId(), attemptProblem.getAttemptProblemId());
public String generateKey(Test test, int problemNumber) {
return String.format("testId:%s:problemNumber:%s",test.getTestId(), problemNumber);
}
public String generateKey(TempCodeDto tempCodeDto) {
return String.format("tests:%s:problems:%s", tempCodeDto.testId, tempCodeDto.attemptProblemId);
return String.format("testId:%s:problemNumber:%s", tempCodeDto.testId, tempCodeDto.getProblemNumber());
}
}

0 comments on commit cf826a1

Please sign in to comment.