-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...hreedollar-domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...domain/src/main/java/com/threedollar/domain/redis/sticker/StickerCountRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...src/main/java/com/threedollar/domain/redis/sticker/repository/StickerCountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |