Skip to content

Commit

Permalink
[feat] RedisRankService를 이용해 캐시하여 랭킹을 가져오도록 수정 (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
kseysh committed Feb 6, 2025
1 parent 3fc0643 commit a5ddeb9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 18 deletions.
30 changes: 30 additions & 0 deletions src/main/java/org/sopt/app/application/rank/RankCacheService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.sopt.app.application.rank;

import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import org.sopt.app.application.soptamp.SoptampUserInfo;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;

public interface RankCacheService {
Set<TypedTuple<Long>> getRanking();

void createNewRank(Long userId);

void removeRank(Long userId);

void incrementScore(Long userId, int score);

void decreaseScore(Long userId, int score);

void initScore(Long userId);

void deleteAll();

void addAll(List<SoptampUserInfo> userInfos);

@Nullable
CachedUserInfo getUserInfo(Long id);

void updateCachedUserInfo(Long id, CachedUserInfo userInfo);
}
81 changes: 63 additions & 18 deletions src/main/java/org/sopt/app/application/rank/RedisRankService.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,97 @@
package org.sopt.app.application.rank;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.sopt.app.application.soptamp.SoptampUserInfo;
import org.sopt.app.common.config.CacheType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class RedisRankService {
private final RedisTemplate<String, String> redisRankingTemplate;
public class RedisRankService implements RankCacheService{
private final RedisTemplate<String, Long> redisTemplate;

public void createNewRank(String nickname) {
redisRankingTemplate.opsForZSet().add(CacheType.SOPTAMP_SCORE.getCacheName(), nickname, 0);
@Override
public Set<TypedTuple<Long>> getRanking() {
try {
return redisTemplate.opsForZSet()
.reverseRangeWithScores(CacheType.SOPTAMP_SCORE.getCacheName(), 0, -1);
}catch (Exception e){
log.warn("Redis에서 Soptamp 랭킹 조회 중 오류 발생", e);
return Collections.emptySet();
}
}

public void removeRank(String nickname) {
redisRankingTemplate.opsForZSet().remove(CacheType.SOPTAMP_SCORE.getCacheName(), nickname);
@Override
public void createNewRank(Long userId) {
redisTemplate.opsForZSet().add(CacheType.SOPTAMP_SCORE.getCacheName(), userId, 0);
}

public void incrementScore(String nickname, int score) {
redisRankingTemplate.opsForZSet().incrementScore(CacheType.SOPTAMP_SCORE.getCacheName(), nickname, score);
@Override
public void removeRank(Long userId) {
redisTemplate.opsForZSet().remove(CacheType.SOPTAMP_SCORE.getCacheName(), userId);
}

public void decreaseScore(String nickname, int score) {
redisRankingTemplate.opsForZSet().incrementScore(CacheType.SOPTAMP_SCORE.getCacheName(), nickname, -1 * score);
@Override
public void incrementScore(Long userId, int score) {
redisTemplate.opsForZSet()
.incrementScore(CacheType.SOPTAMP_SCORE.getCacheName(), userId, score);
}

public void initScore(String nickname) {
redisRankingTemplate.opsForZSet().add(CacheType.SOPTAMP_SCORE.getCacheName(), nickname, 0);
@Override
public void decreaseScore(Long userId, int score) {
redisTemplate.opsForZSet()
.incrementScore(CacheType.SOPTAMP_SCORE.getCacheName(), userId, -1 * score);
}

@Override
public void initScore(Long userId) {
redisTemplate.opsForZSet()
.add(CacheType.SOPTAMP_SCORE.getCacheName(), userId, 0);
}

@Override
public void deleteAll() {
redisRankingTemplate.delete(CacheType.SOPTAMP_SCORE.getCacheName());
redisTemplate.delete(CacheType.SOPTAMP_SCORE.getCacheName());
}

@Override
public void updateCachedUserInfo(Long id, CachedUserInfo userInfo){
redisTemplate.opsForHash()
.put(CacheType.SOPTAMP_PROFILE_MESSAGE.getCacheName(), id, userInfo);
}

@Override
public CachedUserInfo getUserInfo(Long id) {
try {
return (CachedUserInfo) redisTemplate.opsForHash()
.get(CacheType.SOPTAMP_PROFILE_MESSAGE.getCacheName(), id);
} catch (Exception e) {
log.warn("Redis에서 프로필 메시지를 가져오는 중 오류 발생 (userId: {})", id, e);
return null;
}
}

public void addAll(List<SoptampUserInfo> userInfos){
Set<TypedTuple<String>> scores = this.convertRankingSet(userInfos);
redisRankingTemplate.opsForZSet().add(CacheType.SOPTAMP_SCORE.getCacheName(), scores);
@Override
public void addAll(List<SoptampUserInfo> userInfos) {
try {
Set<TypedTuple<Long>> scores = this.convertRankingSet(userInfos);
redisTemplate.opsForZSet().add(CacheType.SOPTAMP_SCORE.getCacheName(), scores);
} catch (Exception e) {
log.warn("Redis에 랭킹 데이터를 추가하는 중 오류 발생", e);
}
}

private Set<TypedTuple<String>> convertRankingSet(List<SoptampUserInfo> userInfos){
private Set<TypedTuple<Long>> convertRankingSet(List<SoptampUserInfo> userInfos){
return userInfos.stream()
.map(user -> TypedTuple.of(user.getNickname(), user.getTotalPoints().doubleValue()))
.map(user -> TypedTuple.of(user.getUserId(), user.getTotalPoints().doubleValue()))
.collect(Collectors.toSet());
}
}

0 comments on commit a5ddeb9

Please sign in to comment.