-
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.
Merge pull request #203 from bucket-back/OMCT-271-feed-ranking-redis
[OMCT-271] 피드 랭킹 기능 추가
- Loading branch information
Showing
7 changed files
with
176 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
17 changes: 17 additions & 0 deletions
17
...ain/java/com/programmers/bucketback/domains/feed/api/response/FeedGetRankingResponse.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,17 @@ | ||
package com.programmers.bucketback.domains.feed.api.response; | ||
|
||
import java.util.List; | ||
|
||
import com.programmers.bucketback.domains.feed.application.dto.response.FeedGetRankingServiceResponse; | ||
import com.programmers.bucketback.redis.feed.model.FeedRankingInfo; | ||
|
||
public record FeedGetRankingResponse( | ||
List<FeedRankingInfo> feedRankingInfos | ||
) { | ||
|
||
public static FeedGetRankingResponse from(final FeedGetRankingServiceResponse serviceResponse) { | ||
List<FeedRankingInfo> feedRankingInfos = serviceResponse.feedRankingInfos(); | ||
|
||
return new FeedGetRankingResponse(feedRankingInfos); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
...mmers/bucketback/domains/feed/application/dto/response/FeedGetRankingServiceResponse.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,10 @@ | ||
package com.programmers.bucketback.domains.feed.application.dto.response; | ||
|
||
import java.util.List; | ||
|
||
import com.programmers.bucketback.redis.feed.model.FeedRankingInfo; | ||
|
||
public record FeedGetRankingServiceResponse( | ||
List<FeedRankingInfo> feedRankingInfos | ||
) { | ||
} |
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
60 changes: 60 additions & 0 deletions
60
...-infrastructure/src/main/java/com/programmers/bucketback/redis/feed/FeedRedisManager.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,60 @@ | ||
package com.programmers.bucketback.redis.feed; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.springframework.data.redis.core.HashOperations; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ZSetOperations; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.programmers.bucketback.redis.feed.model.FeedRankingInfo; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class FeedRedisManager { | ||
|
||
private static final String FEED_RANKING_INFO_HASH_KEY = "feedRankingInfoHashKey"; | ||
private static final String FEED_RANKING_INFO_SET_KEY = "feedRankingInfoSetKey"; | ||
|
||
private final RedisTemplate<String, Object> redisTemplate; | ||
|
||
public List<FeedRankingInfo> getFeedRanking() { | ||
ZSetOperations<String, Object> ZSetOperations = redisTemplate.opsForZSet(); | ||
|
||
Set<ZSetOperations.TypedTuple<Object>> feedRankingInfoSet = | ||
ZSetOperations.reverseRangeWithScores( | ||
FEED_RANKING_INFO_SET_KEY, 0, 9 | ||
); | ||
|
||
return feedRankingInfoSet.stream() | ||
.map(FeedRankingInfo::of) | ||
.toList(); | ||
} | ||
|
||
public boolean isFeedExist(final Long feedId) { | ||
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); | ||
|
||
return hash.hasKey(FEED_RANKING_INFO_HASH_KEY, feedId); | ||
} | ||
|
||
public void changePopularity(final FeedRankingInfo feedRankingInfo, final int value) { | ||
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); | ||
hash.put(FEED_RANKING_INFO_HASH_KEY, feedRankingInfo.feedId(), feedRankingInfo); | ||
|
||
redisTemplate.opsForZSet().incrementScore(FEED_RANKING_INFO_SET_KEY, feedRankingInfo, value); | ||
} | ||
|
||
public void changePopularity(final Long feedId, final int value) { | ||
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); | ||
|
||
if (!hash.hasKey(FEED_RANKING_INFO_HASH_KEY, feedId)) { | ||
return; | ||
} | ||
|
||
FeedRankingInfo feedRankingInfo = (FeedRankingInfo)hash.get(FEED_RANKING_INFO_HASH_KEY, feedId); | ||
changePopularity(feedRankingInfo, value); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...astructure/src/main/java/com/programmers/bucketback/redis/feed/model/FeedRankingInfo.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,30 @@ | ||
package com.programmers.bucketback.redis.feed.model; | ||
|
||
import org.springframework.data.redis.core.ZSetOperations; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record FeedRankingInfo( | ||
|
||
Long feedId, | ||
String hobbyName, | ||
String feedContent, | ||
String bucketName, | ||
int likeCount | ||
|
||
) { | ||
public static FeedRankingInfo of( | ||
final ZSetOperations.TypedTuple<Object> objectTypedTuple | ||
) { | ||
FeedRankingInfo response = (FeedRankingInfo)objectTypedTuple.getValue(); | ||
|
||
return FeedRankingInfo.builder() | ||
.feedId(response.feedId()) | ||
.hobbyName(response.hobbyName()) | ||
.feedContent(response.feedContent()) | ||
.bucketName(response.bucketName()) | ||
.likeCount(response.likeCount()) | ||
.build(); | ||
} | ||
} |