游戏排行榜增加一分钟的缓存
This commit is contained in:
parent
fe77a3519c
commit
a4b80f22fd
@ -1,6 +1,8 @@
|
||||
package com.red.circle.other.app.command.game.ranking;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.red.circle.component.redis.RedisKeys;
|
||||
import com.red.circle.component.redis.service.RedisService;
|
||||
import com.red.circle.framework.dto.PageResult;
|
||||
import com.red.circle.other.app.convertor.game.GameRankingConvertor;
|
||||
import com.red.circle.other.app.dto.clientobject.game.GameRankingCO;
|
||||
@ -8,11 +10,12 @@ import com.red.circle.other.app.dto.cmd.game.GameRankingQry;
|
||||
import com.red.circle.other.domain.game.GameRankingPeriodType;
|
||||
import com.red.circle.other.domain.game.GameRankingRecord;
|
||||
import com.red.circle.other.domain.gateway.game.GameRankingGateway;
|
||||
import com.red.circle.other.infra.database.cache.key.GameRankingKeys;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@ -27,13 +30,28 @@ public class GameRankingQryExe {
|
||||
|
||||
private final GameRankingGateway gameRankingGateway;
|
||||
private final GameRankingConvertor gameRankingConvertor;
|
||||
private final RedisService redisService;
|
||||
|
||||
/**
|
||||
* 缓存过期时间:30秒
|
||||
*/
|
||||
private static final long CACHE_EXPIRE_SECONDS = 60;
|
||||
|
||||
public PageResult<GameRankingCO> execute(GameRankingQry qry) {
|
||||
String periodType = qry.getPeriodType() == null ? GameRankingPeriodType.ALL.getCode() : qry.getPeriodType();
|
||||
Integer pageNum = qry.getCurrent().intValue();
|
||||
Integer pageSize = qry.getSize().intValue();
|
||||
|
||||
// 查询排行榜数据
|
||||
// 构建缓存 Key
|
||||
String cacheKey = buildCacheKey(periodType, pageNum, pageSize);
|
||||
|
||||
// 尝试从缓存获取
|
||||
PageResult<GameRankingCO> cachedResult = redisService.getStringToObject(cacheKey, PageResult.class);
|
||||
if (cachedResult != null) {
|
||||
return cachedResult;
|
||||
}
|
||||
|
||||
// 缓存未命中,查询数据库
|
||||
PageResult<GameRankingRecord> pageResult = gameRankingGateway.pageRanking(periodType, pageNum, pageSize);
|
||||
|
||||
// 计算排名(从当前页的起始排名开始)
|
||||
@ -52,6 +70,17 @@ public class GameRankingQryExe {
|
||||
result.setTotal(pageResult.getTotal());
|
||||
result.setCurrent(pageResult.getCurrent());
|
||||
result.setSize(pageResult.getSize());
|
||||
|
||||
// 写入缓存(30秒过期)
|
||||
redisService.setString(cacheKey, JSON.toJSONString(result), CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建缓存 Key.
|
||||
*/
|
||||
private String buildCacheKey(String periodType, Integer pageNum, Integer pageSize) {
|
||||
return GameRankingKeys.RANKING_PAGE.getKey(periodType, pageNum, pageSize);
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,9 +22,6 @@ public class GameRankingServiceImpl implements GameRankingService {
|
||||
|
||||
private final GameRankingQryExe gameRankingQryExe;
|
||||
|
||||
private static final String CACHE_NAME = "game:ranking";
|
||||
private static final int CACHE_TTL_MINUTES = 5;
|
||||
|
||||
@Override
|
||||
public PageResult<GameRankingCO> pageRanking(GameRankingQry qry) {
|
||||
return gameRankingQryExe.execute(qry);
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
package com.red.circle.other.infra.database.cache.key;
|
||||
|
||||
import com.red.circle.component.redis.RedisKeys;
|
||||
|
||||
/**
|
||||
* 游戏排行榜缓存 Key.
|
||||
*
|
||||
* @author system
|
||||
*/
|
||||
public enum GameRankingKeys implements RedisKeys {
|
||||
|
||||
/**
|
||||
* 游戏排行榜分页查询
|
||||
*/
|
||||
RANKING_PAGE;
|
||||
|
||||
@Override
|
||||
public String businessPrefix() {
|
||||
return "GAME_RANKING";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String businessKey() {
|
||||
return this.name();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user