diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/game/ranking/GameRankingQryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/game/ranking/GameRankingQryExe.java index 359880c6..ad62f3d3 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/game/ranking/GameRankingQryExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/game/ranking/GameRankingQryExe.java @@ -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 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 cachedResult = redisService.getStringToObject(cacheKey, PageResult.class); + if (cachedResult != null) { + return cachedResult; + } + + // 缓存未命中,查询数据库 PageResult 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); + } } diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/GameRankingServiceImpl.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/GameRankingServiceImpl.java index c1704630..078b15e7 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/GameRankingServiceImpl.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/GameRankingServiceImpl.java @@ -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 pageRanking(GameRankingQry qry) { return gameRankingQryExe.execute(qry); diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/key/GameRankingKeys.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/key/GameRankingKeys.java new file mode 100644 index 00000000..754dfde5 --- /dev/null +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/key/GameRankingKeys.java @@ -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(); + } +}