diff --git a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/game/party3rd/GameHkysRestController.java b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/game/party3rd/GameHkysRestController.java index 8fd62556..4162d643 100644 --- a/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/game/party3rd/GameHkysRestController.java +++ b/rc-service/rc-service-other/other-adapter/src/main/java/com/red/circle/other/adapter/app/game/party3rd/GameHkysRestController.java @@ -31,6 +31,7 @@ import com.red.circle.other.domain.game.GameRankingIncrementCmd; import com.red.circle.other.domain.gateway.game.GameRankingGateway; import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService; +import com.red.circle.other.infra.database.cache.service.other.GameListCacheService; import com.red.circle.other.infra.database.rds.entity.sys.GameListConfig; import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService; import com.red.circle.other.inner.enums.config.EnumConfigKey; @@ -74,7 +75,8 @@ public class GameHkysRestController { private final UserProfileAppConvertor userProfileAppConvertor; private final GameListConfigService gameListConfigService; private final ImGroupClient imGroupClient; - private final GameRankingGateway gameRankingGateway; + private final GameRankingGateway gameRankingGateway; + private final GameListCacheService gameListCacheService; @IgnoreResultResponse @PostMapping("/getUserInfo") @@ -141,7 +143,12 @@ public class GameHkysRestController { cost = BigDecimal.valueOf(5000L); } - GameListConfig gameListConfig = gameListConfigService.getByGameId(cmd.getGameId(), GameOriginEnum.LINGXIAN.name(), sysOrigin); + GameListConfig gameListConfig = gameListCacheService.getByGameIdWithCache( + cmd.getGameId(), + GameOriginEnum.LINGXIAN.name(), + sysOrigin, + () -> gameListConfigService.getByGameId(cmd.getGameId(), GameOriginEnum.LINGXIAN.name(), sysOrigin) + ); if (cmd.getType() != null && cmd.getType() == 2 && cmd.getCoin() >= cost.longValue()) { UserProfileDTO userProfile = userProfileAppConvertor.toUserProfileDTO( userProfileGateway.getByUserId(userId)); diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/GameBaishunServiceImpl.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/GameBaishunServiceImpl.java index 3a02dfeb..d0167bcb 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/GameBaishunServiceImpl.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/GameBaishunServiceImpl.java @@ -35,6 +35,7 @@ import com.red.circle.other.domain.ranking.RankingActivityType; import com.red.circle.other.domain.ranking.RankingCycleType; import com.red.circle.other.domain.ranking.RankingDimension; import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService; +import com.red.circle.other.infra.database.cache.service.other.GameListCacheService; import com.red.circle.other.infra.database.rds.entity.sys.GameListConfig; import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService; import com.red.circle.other.inner.enums.config.EnumConfigKey; @@ -79,8 +80,9 @@ public class GameBaishunServiceImpl implements GameBaishunService { private final ImGroupClient imGroupClient; private final RankingActivityService rankingActivityService; private final GameRankingGateway gameRankingGateway; - @Value("${red-circle.game-rank.gameid}") - private String gameId; + private final GameListCacheService gameListCacheService; + @Value("${red-circle.game-rank.gameid}") + private String gameId; @Override public BaishunResponse getToken(BaishunTokenRequest request) { @@ -187,7 +189,12 @@ public class GameBaishunServiceImpl implements GameBaishunService { .gameId(Long.parseLong(request.getGameId())) .build()); - GameListConfig gameListConfig = gameListConfigService.getByGameId(request.getGameId(), GameOriginEnum.BAISHUN.name(), sysOrigin); + GameListConfig gameListConfig = gameListCacheService.getByGameIdWithCache( + request.getGameId(), + GameOriginEnum.BAISHUN.name(), + sysOrigin, + () -> gameListConfigService.getByGameId(request.getGameId(), GameOriginEnum.BAISHUN.name(), sysOrigin) + ); if (Objects.nonNull(gameListConfig)) { sendBroadcast(request, gameListConfig); diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/HotGameServiceImpl.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/HotGameServiceImpl.java index dfbe5e02..ad4b25b2 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/HotGameServiceImpl.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/service/game/HotGameServiceImpl.java @@ -28,6 +28,7 @@ import com.red.circle.other.domain.game.GameRankingIncrementCmd; import com.red.circle.other.domain.gateway.game.GameRankingGateway; import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService; +import com.red.circle.other.infra.database.cache.service.other.GameListCacheService; import com.red.circle.other.infra.database.rds.entity.sys.GameListConfig; import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService; import com.red.circle.other.inner.enums.config.EnumConfigKey; @@ -75,6 +76,7 @@ public class HotGameServiceImpl implements HotGameService { private final ImGroupClient imGroupClient; private final GameListConfigService gameListConfigService; private final GameRankingGateway gameRankingGateway; + private final GameListCacheService gameListCacheService; @Override public HotGameResponse getUserProfile(HotGameUserProfileQuery query) { @@ -198,7 +200,12 @@ public class HotGameServiceImpl implements HotGameService { } String sysOrigin = UserCredential.parseToken(param.getToken()).getSysOrigin(); - GameListConfig gameListConfig = gameListConfigService.getByGameId(param.getGameId(), GameOriginEnum.HOTGAME.name(), sysOrigin); + GameListConfig gameListConfig = gameListCacheService.getByGameIdWithCache( + param.getGameId(), + GameOriginEnum.HOTGAME.name(), + sysOrigin, + () -> gameListConfigService.getByGameId(param.getGameId(), GameOriginEnum.HOTGAME.name(), sysOrigin) + ); sendBroadcast(param, gameListConfig); //累计游戏排行榜 incGameRankingRecord(param, gameListConfig); diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/other/GameListCacheService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/other/GameListCacheService.java index 21583199..a6775573 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/other/GameListCacheService.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/other/GameListCacheService.java @@ -1,6 +1,9 @@ package com.red.circle.other.infra.database.cache.service.other; +import com.red.circle.other.infra.database.rds.entity.sys.GameListConfig; + import java.util.function.Function; +import java.util.function.Supplier; /** * 游戏列表. @@ -19,4 +22,9 @@ public interface GameListCacheService { */ void remove(String sysOrigin); + /** + * 通过游戏ID获取游戏配置(带缓存). + */ + GameListConfig getByGameIdWithCache(String gameId, String gameOrigin, String sysOrigin, Supplier orElse); + } diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/other/impl/GameListCacheServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/other/impl/GameListCacheServiceImpl.java index 58854090..6be7a2ee 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/other/impl/GameListCacheServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/other/impl/GameListCacheServiceImpl.java @@ -1,9 +1,12 @@ package com.red.circle.other.infra.database.cache.service.other.impl; +import com.fasterxml.jackson.core.type.TypeReference; import com.red.circle.component.redis.service.RedisService; import com.red.circle.other.infra.database.cache.key.GameListKeys; import com.red.circle.other.infra.database.cache.service.other.GameListCacheService; +import com.red.circle.other.infra.database.rds.entity.sys.GameListConfig; import com.red.circle.other.infra.utils.RedisUtils; +import com.red.circle.tool.core.json.JacksonUtils; import com.red.circle.tool.core.text.StringUtils; import java.util.ArrayList; @@ -12,6 +15,7 @@ import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.function.Function; +import java.util.function.Supplier; import lombok.RequiredArgsConstructor; import org.springframework.data.redis.connection.RedisConnection; @@ -53,7 +57,21 @@ public class GameListCacheServiceImpl implements GameListCacheService { strings.forEach(redisService::delete); } - - + @Override + public GameListConfig getByGameIdWithCache(String gameId, String gameOrigin, String sysOrigin, Supplier orElse) { + String key = GameListKeys.GAME_LIST.getKey("config:" + gameOrigin + ":" + gameId + ":" + sysOrigin); + String cachedData = redisService.getString(key); + + if (StringUtils.isNotBlank(cachedData)) { + return JacksonUtils.readValue(cachedData, new TypeReference<>() {}); + } + + GameListConfig config = orElse.get(); + if (config != null) { + redisService.setString(key, JacksonUtils.toJson(config), 1, TimeUnit.HOURS); + } + + return config; + } }