Skip to content

Commit

Permalink
refactor: 봉사 모집글 캐시 업데이트 로직을 개선한다.
Browse files Browse the repository at this point in the history
캐시에서 삭제된 데이터가 없으면 캐시에 데이터를 추가하지 않도록 변경함
  • Loading branch information
hseong3243 committed Mar 15, 2024
1 parent 2174a81 commit 9083f54
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ private boolean isFirstPage(String keyword, LocalDate startDate,
@Transactional
public void closeRecruitment(Long shelterId, Long recruitmentId) {
Recruitment recruitment = getRecruitmentByShelter(shelterId, recruitmentId);
recruitmentCacheRepository.deleteRecruitment(recruitment);
long deleted = recruitmentCacheRepository.deleteRecruitment(recruitment);
recruitment.closeRecruitment();
recruitmentCacheRepository.saveRecruitment(recruitment);
if(deleted > 0) {
recruitmentCacheRepository.saveRecruitment(recruitment);
}
}

@Transactional
Expand All @@ -191,7 +193,7 @@ public void updateRecruitment(
List<String> imageUrls
) {
Recruitment recruitment = getRecruitmentByShelterWithImages(shelterId, recruitmentId);
recruitmentCacheRepository.deleteRecruitment(recruitment);
long deleted = recruitmentCacheRepository.deleteRecruitment(recruitment);

List<String> imagesToDelete = recruitment.findImagesToDelete(imageUrls);
applicationEventPublisher.publishEvent(new ImageDeletionEvent(imagesToDelete));
Expand All @@ -205,7 +207,9 @@ public void updateRecruitment(
content,
imageUrls
);
recruitmentCacheRepository.saveRecruitment(recruitment);
if(deleted > 0) {
recruitmentCacheRepository.saveRecruitment(recruitment);
}
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,40 @@ void exceptionWhenRecruitmentNotFound() {
//then
assertThat(exception).isInstanceOf(RecruitmentNotFoundException.class);
}

@Test
@DisplayName("성공: 캐시 되어있던 봉사 모집글이 아니면 캐시 추가를 호출하지 않는다.")
void doesNotInvokeCacheSave_WhenNotExistsInCache() {
//given
given(recruitmentRepository.findByShelterIdAndRecruitmentId(anyLong(), anyLong()))
.willReturn(Optional.ofNullable(recruitment));
given(recruitmentCacheRepository.deleteRecruitment(any()))
.willReturn(0L);

//when
recruitmentService.closeRecruitment(1L, 1L);

//then
then(recruitmentCacheRepository).should(times(0))
.saveRecruitment(any());
}

@Test
@DisplayName("성공: 캐시 되어있던 봉사 모집글이면 캐시 추가를 호출한다.")
void invokeCacheSave_WhenExistsInCache() {
//given
given(recruitmentRepository.findByShelterIdAndRecruitmentId(anyLong(), anyLong()))
.willReturn(Optional.ofNullable(recruitment));
given(recruitmentCacheRepository.deleteRecruitment(any()))
.willReturn(1L);

//when
recruitmentService.closeRecruitment(1L, 1L);

//then
then(recruitmentCacheRepository).should(times(1)).
saveRecruitment(any());
}
}

@Nested
Expand Down Expand Up @@ -587,6 +621,56 @@ void exceptionWhenRecruitmentNotFound() {
//then
assertThat(exception).isInstanceOf(RecruitmentNotFoundException.class);
}

@Test
@DisplayName("성공: 캐시 되어있던 봉사 모집글이 아니면 캐시 추가를 호출하지 않는다.")
void doesNotInvokeCacheSave_WhenNotExistsInCache() {
//given
String newTitle = recruitment.getTitle() + "a";
LocalDateTime newStartTime = recruitment.getStartTime().plusDays(1);
LocalDateTime newEndTime = recruitment.getEndTime().plusDays(1);
LocalDateTime newDeadline = recruitment.getDeadline().plusDays(1);
int newCapacity = recruitment.getCapacity() + 1;
String newContent = recruitment.getContent() + "a";
List<String> newImageUrls = List.of("a1", "a2");

given(recruitmentRepository.findByShelterIdAndRecruitmentIdWithImages(anyLong(),
anyLong())).willReturn(Optional.ofNullable(recruitment));
given(recruitmentCacheRepository.deleteRecruitment(any())).willReturn(0L);

//when
recruitmentService.updateRecruitment(1L, 1L,
newTitle, newStartTime, newEndTime, newDeadline, newCapacity, newContent,
newImageUrls);

//then
then(recruitmentCacheRepository).should(times(0)).saveRecruitment(any());
}

@Test
@DisplayName("성공: 캐시 되어있던 봉사 모집글이면 캐시 추가를 호출한다.")
void invokeCacheSave_WhenExistsInCache() {
//given
String newTitle = recruitment.getTitle() + "a";
LocalDateTime newStartTime = recruitment.getStartTime().plusDays(1);
LocalDateTime newEndTime = recruitment.getEndTime().plusDays(1);
LocalDateTime newDeadline = recruitment.getDeadline().plusDays(1);
int newCapacity = recruitment.getCapacity() + 1;
String newContent = recruitment.getContent() + "a";
List<String> newImageUrls = List.of("a1", "a2");

given(recruitmentRepository.findByShelterIdAndRecruitmentIdWithImages(anyLong(),
anyLong())).willReturn(Optional.ofNullable(recruitment));
given(recruitmentCacheRepository.deleteRecruitment(any())).willReturn(1L);

//when
recruitmentService.updateRecruitment(1L, 1L,
newTitle, newStartTime, newEndTime, newDeadline, newCapacity, newContent,
newImageUrls);

//then
then(recruitmentCacheRepository).should(times(1)).saveRecruitment(any());
}
}

@Nested
Expand Down

0 comments on commit 9083f54

Please sign in to comment.