Skip to content

Commit

Permalink
[TDC-36] stickerCount 추가 (redis)
Browse files Browse the repository at this point in the history
  • Loading branch information
yerimkoko committed Jan 8, 2024
1 parent 9d54868 commit f2916b1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
@@ -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<StickerCountKey, Long> {

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;
}
}
Original file line number Diff line number Diff line change
@@ -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<StickerCountKey, Long> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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);

}

0 comments on commit f2916b1

Please sign in to comment.