-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feat/#543 로컬 캐시에 좋아요 데이터가 실시간으로 반영되도록 수정 #549
Changes from 1 commit
0ed68c1
64e0a88
44f67c5
21f7976
1fa1b45
d0d8fa2
0f1c9e3
fd5f35e
2c0a4ee
12ae35c
4266541
2373790
1d3fdcc
43c5651
e700811
2e8398a
03654ec
dc73563
358254e
1e18d62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ private void create(final KillingPart killingPart, final Member member) { | |
.orElseGet(() -> createNewLike(killingPart, member)); | ||
if (likeOnKillingPart.isDeleted()) { | ||
inMemorySongs.like(killingPart, likeOnKillingPart); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 결과적으로 DB에 좋아요가 반영되는 코드는 없는 것 같은데, 캐시 데이터에 저장된 좋아요 개수가 언제 DB에 반영되는 건가요? 아직까지는 동시성 테스트를 해봤을 때, DB의 좋아요 개수는 증가/감소하지 않는 것 같아서요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DB 좋아요 개수 증감 로직을 안 짰네요... Scheduler 로 좋아요를 batch update 하는 로직도 만들어 보겠습니다! 좋은 지적 감사합니다 역시 바론이네요 ^^ |
||
likeRepository.pressLike(likeOnKillingPart.getId()); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ public class InMemorySongs { | |
private Map<Long, Song> songs = new HashMap<>(); | ||
private List<Long> sortedSongIds = new ArrayList<>(); | ||
|
||
public void refreshSongs(final List<Song> songs) { | ||
public synchronized void refreshSongs(final List<Song> songs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refreshSongs에 synchronized를 붙인 것은 아마도 #549 (comment) 이 부분 때문인 것 같습니다.
이 부분에 대한 누락을 막으려면 |
||
this.songs = songs.stream() | ||
.collect(Collectors.toMap(Song::getId, song -> song, (prev, update) -> update, HashMap::new)); | ||
this.sortedSongIds = new ArrayList<>(this.songs.keySet().stream() | ||
|
@@ -116,7 +116,16 @@ public void like(final KillingPart killingPart, final KillingPartLike likeOnKill | |
} | ||
} | ||
|
||
private static KillingPart findKillingPart(final KillingPart killingPart, final Song song) { | ||
public void unlike(final KillingPart killingPart, final KillingPartLike unlikeOnKillingPart) { | ||
final Song song = songs.get(killingPart.getSong().getId()); | ||
final KillingPart killingPartById = findKillingPart(killingPart, song); | ||
final boolean updated = killingPartById.unlike(unlikeOnKillingPart); | ||
if (updated) { | ||
reorder(song); | ||
} | ||
} | ||
|
||
private KillingPart findKillingPart(final KillingPart killingPart, final Song song) { | ||
return song.getKillingParts().stream() | ||
.filter(kp -> kp.equals(killingPart)) | ||
.findAny() | ||
|
@@ -126,14 +135,16 @@ private static KillingPart findKillingPart(final KillingPart killingPart, final | |
} | ||
|
||
private void reorder(final Song updatedSong) { | ||
int currentSongIndex = sortedSongIds.indexOf(updatedSong.getId()); | ||
synchronized (sortedSongIds) { | ||
int currentSongIndex = sortedSongIds.indexOf(updatedSong.getId()); | ||
|
||
if (currentSongIndex == -1) { | ||
return; | ||
} | ||
if (currentSongIndex == -1) { | ||
return; | ||
} | ||
|
||
moveForward(updatedSong, currentSongIndex); | ||
moveBackward(updatedSong, currentSongIndex); | ||
moveForward(updatedSong, currentSongIndex); | ||
moveBackward(updatedSong, currentSongIndex); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reorder하는 부분을 synchronized하는 것 👍👍 |
||
} | ||
|
||
private void moveForward(final Song changedSong, final int songIndex) { | ||
|
@@ -186,13 +197,4 @@ private boolean shouldSwapWithNext(final Song song, final Song nextSong) { | |
|
||
return hasSmallerTotalLikeCountThanNextSong || hasSameTotalLikeCountAndSmallerIdThanNextSong; | ||
} | ||
|
||
public void unlike(final KillingPart killingPart, final KillingPartLike unlikeOnKillingPart) { | ||
final Song song = songs.get(killingPart.getSong().getId()); | ||
final KillingPart killingPartById = findKillingPart(killingPart, song); | ||
final boolean updated = killingPartById.unlike(unlikeOnKillingPart); | ||
if (updated) { | ||
reorder(song); | ||
} | ||
} | ||
} |
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.
그리구 한 번 merge 한 다음에 다시 KillingPart를 detach 해주는 작업도 필요할 것 같은데 이건 따로 작성하지 않아도 되는건가용?.? (몰라서 하는 질문임니닷)
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.
저도 이 부분이 궁금하네요! 따로 다시 detach를 안해도 괜찮은지 궁금합니다.
트랜잭션이 끝나면 영속성 컨텍스트도 같이 종료되니까 따로 안해도 괜찮을 것 같기도 하네요 (저도 이 부분을 잘 모르겠습니다.)
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.
저 궁금한 것이 있는데 updateCachedSong이 동작하는 중간에 killingPart에 좋아요가 발생하는 상황은 따로 고려하지 않아도 되는 것인지도 궁금합니다. (updateCachedSong의 경우 영속성 컨텍스트로 정보를 merge하기에 이 때 좋아요가 발생해도 문제가 없는 것인지 궁금했습니다.)
제가 detach에 대한 개념이 부족해서 이상한 질문일 수도 있습니다..
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.
동시성 관련된 부분은 당시 코드에서 고려되지 않았기 때문에 아코가 말씀하셨던 것처럼 updateCacheSong 이 동작하는 중간에 killingPart 에 좋아요가 발생하면 좋아요가 유실될 가능성이 있습니다. synchronized 키워드로 동시성을 보장하도록 코드를 작성했습니다!
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.
그리고 원래라면 detach 를 해야 하지만, 위에서 말씀드린 것처럼 좋아요 데이터의 deleted 여부는 실시간으로 DB 에 반영되어야 할 것 같아서 하지 않았습니다...!
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.
detach를 하지 않는 이유를 혹시 조금만 더 자세하게 설명해주실 수 있을까요? 제가 잘 이해를 하지 못해서 그렇습니다!🥲