缓存处理
This commit is contained in:
parent
bae054b23c
commit
69b85196b6
@ -16,6 +16,7 @@ import com.red.circle.mq.rocket.business.producer.UserMqMessageService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
@ -45,8 +46,9 @@ public class RankingActivityServiceImpl implements RankingActivityService {
|
||||
private final UserMqMessageService userMqMessageService;
|
||||
private final UserProfileGateway userProfileGateway;
|
||||
|
||||
private static final String RANKING_TOP4_CACHE_KEY_PREFIX = "ranking_top4:";
|
||||
private static final String RANKING_LIST_CACHE_KEY_PREFIX = "ranking_list:";
|
||||
private static final int CACHE_EXPIRE_MINUTES = 5;
|
||||
private static final String GAME_KING_TEMPLATE_ID = "2001207026499137537";
|
||||
|
||||
@Override
|
||||
public Boolean accumulateRankingData(RankingDataUpdateCmd cmd) {
|
||||
@ -55,7 +57,24 @@ public class RankingActivityServiceImpl implements RankingActivityService {
|
||||
|
||||
@Override
|
||||
public RankingListCO getRankingList(RankingListQueryCmd cmd) {
|
||||
return rankingListQryExe.execute(cmd);
|
||||
if (!Objects.equals(GAME_KING_TEMPLATE_ID, cmd.getTemplateId())) {
|
||||
return rankingListQryExe.execute(cmd);
|
||||
}
|
||||
|
||||
String cacheKey = buildRankingListCacheKey(cmd);
|
||||
|
||||
RankingListCO cachedResult = redisService.getStringToObject(cacheKey, RankingListCO.class);
|
||||
if (cachedResult != null) {
|
||||
return cachedResult;
|
||||
}
|
||||
|
||||
RankingListCO result = rankingListQryExe.execute(cmd);
|
||||
|
||||
if (result != null) {
|
||||
redisService.setString(cacheKey, JSON.toJSONString(result), CACHE_EXPIRE_MINUTES, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -70,11 +89,11 @@ public class RankingActivityServiceImpl implements RankingActivityService {
|
||||
|
||||
@Override
|
||||
public List<RankingUserVO> getTopFourRankingWithReward(RankingListQueryCmd cmd) {
|
||||
String cacheKey = buildCacheKey(cmd);
|
||||
|
||||
String cachedJson = redisService.getString(cacheKey);
|
||||
if (cachedJson != null) {
|
||||
return JSON.parseArray(cachedJson, RankingUserVO.class);
|
||||
String cacheKey = buildRankingListCacheKey(cmd);
|
||||
|
||||
RankingListCO cachedResult = redisService.getStringToObject(cacheKey, RankingListCO.class);
|
||||
if (cachedResult != null) {
|
||||
return convertToRankingUserVOList(cachedResult);
|
||||
}
|
||||
|
||||
RankingListQueryCmd top4Cmd = new RankingListQueryCmd();
|
||||
@ -84,42 +103,54 @@ public class RankingActivityServiceImpl implements RankingActivityService {
|
||||
top4Cmd.setReqUserId(cmd.getReqUserId());
|
||||
top4Cmd.setReqSysOrigin(cmd.getReqSysOrigin());
|
||||
top4Cmd.setTopN(10);
|
||||
top4Cmd.setTemplateId(cmd.getTemplateId());
|
||||
|
||||
RankingListCO result = rankingListQryExe.execute(top4Cmd);
|
||||
|
||||
List<RankingUserVO> rankingUsers = null;
|
||||
if (result != null && CollectionUtils.isNotEmpty(result.getRankingList())) {
|
||||
Set<Long> userIds = result.getRankingList().stream()
|
||||
.map(RankingActivityRecordCO::getUserId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
Map<Long, UserProfile> userProfileMap = userProfileGateway.mapByUserIds(userIds);
|
||||
|
||||
rankingUsers = result.getRankingList().stream()
|
||||
.limit(4)
|
||||
.map(user -> {
|
||||
UserProfile profile = userProfileMap.get(user.getUserId());
|
||||
RankingUserVO vo = new RankingUserVO();
|
||||
vo.setUserId(user.getUserId());
|
||||
vo.setRank(user.getRank());
|
||||
vo.setQuantity(String.valueOf(user.getQuantity()));
|
||||
|
||||
if (profile != null) {
|
||||
vo.setAccount(profile.getAccount());
|
||||
vo.setNickname(profile.getUserNickname());
|
||||
vo.setAvatar(profile.getUserAvatar());
|
||||
}
|
||||
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
redisService.setString(cacheKey, JSON.toJSONString(rankingUsers), CACHE_EXPIRE_MINUTES, TimeUnit.MINUTES);
|
||||
|
||||
redisService.setString(cacheKey, JSON.toJSONString(result), CACHE_EXPIRE_MINUTES, TimeUnit.MINUTES);
|
||||
sendGameKingRewardEvent(cmd, result);
|
||||
return convertToRankingUserVOList(result);
|
||||
}
|
||||
|
||||
return rankingUsers;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换RankingListCO为RankingUserVO列表(只取前4个).
|
||||
*/
|
||||
private List<RankingUserVO> convertToRankingUserVOList(RankingListCO result) {
|
||||
if (result == null || CollectionUtils.isEmpty(result.getRankingList())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<RankingActivityRecordCO> top4Users = result.getRankingList().stream()
|
||||
.limit(4)
|
||||
.toList();
|
||||
|
||||
Set<Long> userIds = top4Users.stream()
|
||||
.map(RankingActivityRecordCO::getUserId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
Map<Long, UserProfile> userProfileMap = userProfileGateway.mapByUserIds(userIds);
|
||||
|
||||
return top4Users.stream()
|
||||
.map(user -> {
|
||||
UserProfile profile = userProfileMap.get(user.getUserId());
|
||||
RankingUserVO vo = new RankingUserVO();
|
||||
vo.setUserId(user.getUserId());
|
||||
vo.setRank(user.getRank());
|
||||
vo.setQuantity(String.valueOf(user.getQuantity()));
|
||||
|
||||
if (profile != null) {
|
||||
vo.setAccount(profile.getAccount());
|
||||
vo.setNickname(profile.getUserNickname());
|
||||
vo.setAvatar(profile.getUserAvatar());
|
||||
}
|
||||
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -145,9 +176,9 @@ public class RankingActivityServiceImpl implements RankingActivityService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建缓存Key.
|
||||
* 构建排行榜缓存Key.
|
||||
*/
|
||||
private String buildCacheKey(RankingListQueryCmd cmd) {
|
||||
return RANKING_TOP4_CACHE_KEY_PREFIX + RankingActivityType.getByCode(cmd.getActivityType()).name() + ":" + cmd.getCycleKey();
|
||||
private String buildRankingListCacheKey(RankingListQueryCmd cmd) {
|
||||
return RANKING_LIST_CACHE_KEY_PREFIX + RankingActivityType.getByCode(cmd.getActivityType()).name() + ":" + cmd.getCycleKey();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user