From 67e8341001bc5fb0228ce5a568553d85170aa527 Mon Sep 17 00:00:00 2001 From: huiseung Date: Mon, 26 Aug 2024 13:23:00 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20=EB=8F=99=EC=9D=BC=20=ED=82=A4=EC=9B=8C?= =?UTF-8?q?=EB=93=9C=EC=99=80=20=EB=8F=99=EC=9D=BC=20id=EA=B0=80=20?= =?UTF-8?q?=EC=98=A4=EB=A9=B4=20=EB=8F=99=EC=9D=BC=20=EA=B4=91=EA=B3=A0?= =?UTF-8?q?=EA=B0=80=20=EC=9D=91=EB=8B=B5=EB=90=98=EA=B2=8C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AdvertisementController.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/service/advertisement-service/src/main/java/woowa/team4/bff/advertisementservice/AdvertisementController.java b/service/advertisement-service/src/main/java/woowa/team4/bff/advertisementservice/AdvertisementController.java index 700cf621..b03bbae6 100644 --- a/service/advertisement-service/src/main/java/woowa/team4/bff/advertisementservice/AdvertisementController.java +++ b/service/advertisement-service/src/main/java/woowa/team4/bff/advertisementservice/AdvertisementController.java @@ -3,6 +3,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; import lombok.Getter; import lombok.Setter; import org.springframework.web.bind.annotation.PostMapping; @@ -18,6 +20,8 @@ public class AdvertisementController { private final Random random = new Random(); + private final ConcurrentHashMap> advertisementCache = new ConcurrentHashMap<>(); + private final ConcurrentHashMap keywordRanks = new ConcurrentHashMap<>(); @PostMapping public List getAdvertisements( @@ -31,9 +35,18 @@ public List getAdvertisements( throw new RuntimeException(e); } + String keyword = request.getKeyword(); + ConcurrentHashMap keywordCache = advertisementCache.computeIfAbsent(keyword, k -> new ConcurrentHashMap<>()); + AtomicInteger rank = keywordRanks.computeIfAbsent(keyword, k -> new AtomicInteger(0)); + for (Long id : request.getIds()) { - int rank = 0; - responses.add(new AdvertisementResponse(id, ++rank, random.nextBoolean())); + AdvertisementResponse response = keywordCache.computeIfAbsent(id, + key -> { + boolean hasAd = random.nextBoolean(); + int adRank = hasAd ? rank.incrementAndGet() : Integer.MAX_VALUE; + return new AdvertisementResponse(key, adRank, hasAd); + }); + responses.add(response); } return responses;