Skip to content

Commit

Permalink
[TDC-43] targetId 로 조회하기 성능 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
yerimkoko committed Jan 17, 2024
1 parent 01c8e95 commit e55cf83
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -49,7 +50,7 @@ public ApiResponse<String> createReaction(@Valid @RequestBody AddReactionRequest
@Operation(summary = "[스티커] 타겟들에 대한 스티커들을 조회합니다")
@GetMapping("v1/sticker-group/{stickerGroup}/stickers/targetIds")
public ApiResponse<List<TargetStickerReactionResponse>> getTargetStickerReactions(@PathVariable StickerGroup stickerGroup,
@RequestParam List<String> targetIds,
@RequestParam Set<String> targetIds,
@RequestParam String accountId) {
return ApiResponse.success(stickerReactionRetrieveService.getStickerReactionResponse(stickerGroup, accountId, targetIds));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.threedollar.service.sticker;

import com.threedollar.domain.redis.sticker.StickerCountKey;
import com.threedollar.domain.stickeraction.StickerAction;
import com.threedollar.domain.stickeraction.repository.StickerActionRepository;
import com.threedollar.domain.redis.sticker.repository.StickerCountRepository;
import com.threedollar.domain.sticker.Sticker;
import com.threedollar.domain.sticker.StickerGroup;
import com.threedollar.domain.sticker.repository.StickerRepository;
import com.threedollar.service.sticker.dto.response.StickerInfoDetail;
import com.threedollar.service.sticker.dto.response.TargetStickerReactionResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
Expand All @@ -29,33 +31,52 @@ public class StickerReactionRetrieveService {
@Transactional(readOnly = true)
public List<TargetStickerReactionResponse> getStickerReactionResponse(StickerGroup stickerGroup,
String accountId,
List<String> targetIds) {
Set<String> targetIds) {

List<Sticker> stickers = stickerRepository.getStickerByStickerGroup(stickerGroup);

Map<String, List<StickerAction>> reactionMap = stickerActionRepository.getReactionByStickerGroupAndTargetIds(stickerGroup, targetIds);
Map<StickerCountKey, Long> stickerCountKeyLongMap = getStickerCountKey(stickerGroup, targetIds, stickers);

List<TargetStickerReactionResponse> targetStickerReactionResponses = new ArrayList<>();
Map<String, StickerAction> targetIdActedByMe = getTargetIdActedByMe(targetIds, accountId, stickerGroup);

for (String targetId : targetIds) {
targetStickerReactionResponses.addAll(reactionMap.get(targetId).stream()
.map(
reaction -> TargetStickerReactionResponse.of(isSelected(reaction, accountId), targetId, stickerGroup, stickerCountRepository,
getStickers(stickers, reaction, targetId))).toList());
}
return targetStickerReactionResponses;
return targetIds.stream()
.map(targetId -> {
List<StickerInfoDetail> stickerInfoDetails = stickers.stream().map(

sticker -> {
long count = stickerCountKeyLongMap.getOrDefault(StickerCountKey.of(stickerGroup, targetId, sticker.getId()), 0L);
StickerAction stickerAction = targetIdActedByMe.getOrDefault(targetId, null);
return StickerInfoDetail.of(sticker,
count,
stickerAction == null || stickerAction.getStickerIds().contains(sticker.getId()));

}).toList();
return TargetStickerReactionResponse.builder()
.stickerInfoDetailList(stickerInfoDetails)
.targetId(targetId)
.build();
}).collect(Collectors.toList());

}

public static boolean isSelected(StickerAction stickerAction, String accountId) {
return stickerAction.getAccountId().equals(accountId);
private Map<StickerCountKey, Long> getStickerCountKey(StickerGroup stickerGroup,
Set<String> targetIds,
List<Sticker> stickers) {
List<StickerCountKey> stickerCountKeys = targetIds.stream()
.flatMap(targetId -> stickers.stream()
.map(sticker -> StickerCountKey.of(stickerGroup, targetId, sticker.getId()))
).toList();
return stickerCountRepository.stickerCount(stickerCountKeys);

}

public static List<Sticker> getStickers(List<Sticker> stickers, StickerAction stickerAction, String targetId) {
return stickers.stream()
.filter(sticker -> sticker.getStickerGroup().equals(stickerAction.getStickerGroup()) &&
targetId.equals(stickerAction.getTargetId()))
.collect(Collectors.toList());
private Map<String, StickerAction> getTargetIdActedByMe(Set<String> targetIds,
String accountId,
StickerGroup stickerGroup) {
List<StickerAction> stickerActions = stickerActionRepository.getStickerActionByMe(accountId, targetIds, stickerGroup);
return stickerActions.stream()
.collect(Collectors.toMap(StickerAction::getTargetId, stickerAction -> stickerAction));

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.threedollar.service.sticker.dto.response;

import com.threedollar.domain.redis.sticker.repository.StickerCountRepository;
import com.threedollar.domain.sticker.Sticker;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -29,13 +28,12 @@ public StickerInfoDetail(Long stickerId, Long stickerCount, String imageUrl, int
this.selectedByMe = selectedByMe;
}

public static StickerInfoDetail of(boolean isSelected, Sticker sticker, String targetId, StickerCountRepository stickerCountRepository) {
Long count = stickerCountRepository.getValueByKey(sticker.getStickerGroup(), targetId, sticker.getId());
public static StickerInfoDetail of(Sticker sticker, Long stickerCount, boolean isSelected) {
return StickerInfoDetail.builder()
.stickerId(sticker.getId())
.priority(sticker.getPriority())
.imageUrl(sticker.getImageUrl())
.stickerCount(count)
.stickerCount(stickerCount)
.selectedByMe(isSelected)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public StickerCountKey(StickerGroup stickerGroup, String targetId, Long stickerI
this.stickerId = stickerId;
}

public static StickerCountKey of(StickerGroup stickerGroup, String targetId, Long stickerId) {
return StickerCountKey.builder()
.stickerId(stickerId)
.stickerGroup(stickerGroup)
.targetId(targetId)
.build();
}

@Override
public String getKey() {
return "stickerGroup:" + stickerGroup + "," + "targetId:" + targetId + "," + "stickerId:" + stickerId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Repository
Expand Down Expand Up @@ -45,10 +47,10 @@ public void decrByCount(StickerGroup stickerGroup, String targetId, Long sticker
public void incrBulkByCount(StickerGroup stickerGroup, String targetId, List<Long> stickerIds) {
List<StickerCountKey> stickerCountKeys = stickerIds.stream()
.map(id -> StickerCountKey.builder()
.stickerId(id)
.targetId(targetId)
.stickerGroup(stickerGroup)
.build())
.stickerId(id)
.targetId(targetId)
.stickerGroup(stickerGroup)
.build())
.toList();
stickerRedisRepository.incrBulk(stickerCountKeys);

Expand Down Expand Up @@ -77,4 +79,14 @@ public List<Long> getValuesByKeys(StickerGroup stickerGroup, String targetId, Li
.toList();
return stickerRedisRepository.getBulk(stickerCountKeys);
}

@Override
public Map<StickerCountKey, Long> stickerCount(List<StickerCountKey> stickerCountKeys) {
Map<StickerCountKey, Long> result = new HashMap<>();
for (StickerCountKey stickerCountKey : stickerCountKeys) {
Long count = stickerRedisRepository.get(stickerCountKey);
result.put(stickerCountKey, count);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.threedollar.domain.redis.sticker.repository;

import com.threedollar.domain.redis.sticker.StickerCountKey;
import com.threedollar.domain.sticker.StickerGroup;

import java.util.List;
import java.util.Map;

public interface StickerCountRepository {

Expand All @@ -18,6 +20,8 @@ public interface StickerCountRepository {

List<Long> getValuesByKeys(StickerGroup stickerGroup, String targetId, List<Long> stickerIds);

Map<StickerCountKey, Long> stickerCount(List<StickerCountKey> stickerCountKeys);



}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.threedollar.domain.sticker.StickerGroup;

import java.util.List;
import java.util.Map;
import java.util.Set;


public interface StickerActionRepositoryCustom {
Expand All @@ -14,7 +14,9 @@ StickerAction getReactionByStickerGroupAndTargetIdAndAccountId(StickerGroup stic
String accountId);


Map<String, List<StickerAction>> getReactionByStickerGroupAndTargetIds(StickerGroup stickerGroup,
List<String> targetIds);

List<StickerAction> getStickerActionByMe(String accountId,
Set<String> targetIds,
StickerGroup stickerGroup);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import com.threedollar.domain.sticker.StickerGroup;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.threedollar.domain.stickeraction.QStickerAction.stickerAction;

Expand All @@ -27,22 +26,15 @@ public StickerAction getReactionByStickerGroupAndTargetIdAndAccountId(StickerGro
.fetchOne();
}


@Override
public Map<String, List<StickerAction>> getReactionByStickerGroupAndTargetIds(StickerGroup stickerGroup, List<String> targetIds) {
Map<String, List<StickerAction>> reactionMap = new HashMap<>();
for (String targetId : targetIds) {
reactionMap.put(targetId, getReactionsByTargetId(targetId, stickerGroup));
}
return reactionMap;
}

private List<StickerAction> getReactionsByTargetId(String targetId, StickerGroup stickerGroup) {
public List<StickerAction> getStickerActionByMe(String accountId, Set<String> targetIds, StickerGroup stickerGroup) {
return jpaQueryFactory.selectFrom(stickerAction)
.where(stickerAction.targetId.eq(targetId),
stickerAction.stickerGroup.eq(stickerGroup))
.where(stickerAction.stickerGroup.eq(stickerGroup),
stickerAction.targetId.in(targetIds),
stickerAction.accountId.eq(accountId))
.fetch();
}


}

0 comments on commit e55cf83

Please sign in to comment.