From a4b80f22fdfdd332443c5922b51c2f44761f32b8 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Tue, 2 Dec 2025 19:10:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=B8=E6=88=8F=E6=8E=92=E8=A1=8C=E6=A6=9C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E5=88=86=E9=92=9F=E7=9A=84=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../game/ranking/GameRankingQryExe.java | 35 +++++++++++++++++-- .../service/game/GameRankingServiceImpl.java | 3 -- .../database/cache/key/GameRankingKeys.java | 26 ++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/key/GameRankingKeys.java 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(); + } +}