From f2916b15d0959e0fc2050ee6fb4475a61b77501b Mon Sep 17 00:00:00 2001 From: yerimkoko Date: Mon, 8 Jan 2024 21:56:06 +0900 Subject: [PATCH] =?UTF-8?q?[TDC-36]=20stickerCount=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?(redis)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sticker/StickerReactionService.java | 3 ++ .../domain/redis/sticker/StickerCountKey.java | 51 +++++++++++++++++++ .../sticker/StickerCountRepositoryImpl.java | 41 +++++++++++++++ .../repository/StickerCountRepository.java | 13 +++++ 4 files changed, 108 insertions(+) create mode 100644 threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountKey.java create mode 100644 threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountRepositoryImpl.java create mode 100644 threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/repository/StickerCountRepository.java diff --git a/threedollar-application/src/main/java/com/threedollar/service/sticker/StickerReactionService.java b/threedollar-application/src/main/java/com/threedollar/service/sticker/StickerReactionService.java index 555a339..e1e89ee 100644 --- a/threedollar-application/src/main/java/com/threedollar/service/sticker/StickerReactionService.java +++ b/threedollar-application/src/main/java/com/threedollar/service/sticker/StickerReactionService.java @@ -3,6 +3,7 @@ import com.threedollar.domain.reaction.Reaction; import com.threedollar.domain.reaction.repository.ReactionRepository; +import com.threedollar.domain.redis.sticker.repository.StickerCountRepository; import com.threedollar.domain.sticker.StickerGroup; import com.threedollar.domain.sticker.repository.StickerRepository; import com.threedollar.service.sticker.request.AddReactionRequest; @@ -20,6 +21,8 @@ public class StickerReactionService { private final ReactionRepository reactionRepository; private final StickerRepository stickerRepository; + private final StickerCountRepository stickerCountRepository; + @Transactional public void upsertSticker(AddReactionRequest request, StickerGroup stickerGroup) { diff --git a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountKey.java b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountKey.java new file mode 100644 index 0000000..62fcf2b --- /dev/null +++ b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountKey.java @@ -0,0 +1,51 @@ +package com.threedollar.domain.redis.sticker; + +import com.threedollar.common.exception.util.JsonUtils; +import com.threedollar.domain.redis.StringRedisKey; +import com.threedollar.domain.sticker.StickerGroup; +import lombok.Builder; +import lombok.Getter; + +import java.time.Duration; + +@Getter +public class StickerCountKey implements StringRedisKey { + + private static final long DEFAULT_VALUE = 0L; + + private final StickerGroup stickerGroup; + + private final String targetId; + + @Builder + public StickerCountKey(StickerGroup stickerGroup, String targetId) { + this.stickerGroup = stickerGroup; + this.targetId = targetId; + } + + @Override + public String getKey() { + return "stickerGroup : " + stickerGroup + "targetId : " + targetId; + } + + @Override + public Long deserializeValue(String value) { + if (value == null) { + return DEFAULT_VALUE; + } try { + return Long.valueOf(value); + } catch (Exception e) { + throw new IllegalArgumentException(String.format("역직렬화 중 에러가 발생하였습니다. value: (%s)", value)); + } + } + + @Override + public String serializeValue(Long value) { + return JsonUtils.toJson(value); + } + + @Override + public Duration getTtl() { + return null; + } +} diff --git a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountRepositoryImpl.java b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountRepositoryImpl.java new file mode 100644 index 0000000..281d767 --- /dev/null +++ b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountRepositoryImpl.java @@ -0,0 +1,41 @@ +package com.threedollar.domain.redis.sticker; + +import com.threedollar.domain.redis.StringRedisRepository; +import com.threedollar.domain.redis.sticker.repository.StickerCountRepository; +import com.threedollar.domain.sticker.StickerGroup; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; + +@RequiredArgsConstructor +@Repository +public class StickerCountRepositoryImpl implements StickerCountRepository { + + private final StringRedisRepository stickerRedisRepository; + + @Override + public void incrByCount(StickerGroup stickerGroup, String targetId) { + StickerCountKey key = StickerCountKey.builder() + .stickerGroup(stickerGroup) + .targetId(targetId) + .build(); + stickerRedisRepository.incr(key); + } + + @Override + public long getValueByKey(StickerGroup stickerGroup, String targetId) { + StickerCountKey key = StickerCountKey.builder() + .stickerGroup(stickerGroup) + .targetId(targetId) + .build(); + return stickerRedisRepository.get(key); + } + + @Override + public void decrByCount(StickerGroup stickerGroup, String targetId) { + StickerCountKey key = StickerCountKey.builder() + .stickerGroup(stickerGroup) + .targetId(targetId) + .build(); + stickerRedisRepository.decr(key); + } +} diff --git a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/repository/StickerCountRepository.java b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/repository/StickerCountRepository.java new file mode 100644 index 0000000..e06831d --- /dev/null +++ b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/repository/StickerCountRepository.java @@ -0,0 +1,13 @@ +package com.threedollar.domain.redis.sticker.repository; + +import com.threedollar.domain.sticker.StickerGroup; + +public interface StickerCountRepository { + + void incrByCount(StickerGroup stickerGroup, String targetId); + + long getValueByKey(StickerGroup stickerGroup, String targetId); + + void decrByCount(StickerGroup stickerGroup, String targetId); + +}