游戏中奖增加gameId缓存

This commit is contained in:
tianfeng 2025-12-03 15:27:52 +08:00
parent bb4a0e1ec2
commit a1f88ffc5c
5 changed files with 55 additions and 8 deletions

View File

@ -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.game.GameRankingGateway;
import com.red.circle.other.domain.gateway.user.UserProfileGateway; 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.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.entity.sys.GameListConfig;
import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService; import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService;
import com.red.circle.other.inner.enums.config.EnumConfigKey; import com.red.circle.other.inner.enums.config.EnumConfigKey;
@ -74,7 +75,8 @@ public class GameHkysRestController {
private final UserProfileAppConvertor userProfileAppConvertor; private final UserProfileAppConvertor userProfileAppConvertor;
private final GameListConfigService gameListConfigService; private final GameListConfigService gameListConfigService;
private final ImGroupClient imGroupClient; private final ImGroupClient imGroupClient;
private final GameRankingGateway gameRankingGateway; private final GameRankingGateway gameRankingGateway;
private final GameListCacheService gameListCacheService;
@IgnoreResultResponse @IgnoreResultResponse
@PostMapping("/getUserInfo") @PostMapping("/getUserInfo")
@ -141,7 +143,12 @@ public class GameHkysRestController {
cost = BigDecimal.valueOf(5000L); 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()) { if (cmd.getType() != null && cmd.getType() == 2 && cmd.getCoin() >= cost.longValue()) {
UserProfileDTO userProfile = userProfileAppConvertor.toUserProfileDTO( UserProfileDTO userProfile = userProfileAppConvertor.toUserProfileDTO(
userProfileGateway.getByUserId(userId)); userProfileGateway.getByUserId(userId));

View File

@ -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.RankingCycleType;
import com.red.circle.other.domain.ranking.RankingDimension; 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.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.entity.sys.GameListConfig;
import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService; import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService;
import com.red.circle.other.inner.enums.config.EnumConfigKey; import com.red.circle.other.inner.enums.config.EnumConfigKey;
@ -79,8 +80,9 @@ public class GameBaishunServiceImpl implements GameBaishunService {
private final ImGroupClient imGroupClient; private final ImGroupClient imGroupClient;
private final RankingActivityService rankingActivityService; private final RankingActivityService rankingActivityService;
private final GameRankingGateway gameRankingGateway; private final GameRankingGateway gameRankingGateway;
@Value("${red-circle.game-rank.gameid}") private final GameListCacheService gameListCacheService;
private String gameId; @Value("${red-circle.game-rank.gameid}")
private String gameId;
@Override @Override
public BaishunResponse<BaishunTokenResponse> getToken(BaishunTokenRequest request) { public BaishunResponse<BaishunTokenResponse> getToken(BaishunTokenRequest request) {
@ -187,7 +189,12 @@ public class GameBaishunServiceImpl implements GameBaishunService {
.gameId(Long.parseLong(request.getGameId())) .gameId(Long.parseLong(request.getGameId()))
.build()); .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)) { if (Objects.nonNull(gameListConfig)) {
sendBroadcast(request, gameListConfig); sendBroadcast(request, gameListConfig);

View File

@ -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.game.GameRankingGateway;
import com.red.circle.other.domain.gateway.user.UserProfileGateway; 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.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.entity.sys.GameListConfig;
import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService; import com.red.circle.other.infra.database.rds.service.sys.GameListConfigService;
import com.red.circle.other.inner.enums.config.EnumConfigKey; import com.red.circle.other.inner.enums.config.EnumConfigKey;
@ -75,6 +76,7 @@ public class HotGameServiceImpl implements HotGameService {
private final ImGroupClient imGroupClient; private final ImGroupClient imGroupClient;
private final GameListConfigService gameListConfigService; private final GameListConfigService gameListConfigService;
private final GameRankingGateway gameRankingGateway; private final GameRankingGateway gameRankingGateway;
private final GameListCacheService gameListCacheService;
@Override @Override
public HotGameResponse<HotGameUserProfile> getUserProfile(HotGameUserProfileQuery query) { public HotGameResponse<HotGameUserProfile> getUserProfile(HotGameUserProfileQuery query) {
@ -198,7 +200,12 @@ public class HotGameServiceImpl implements HotGameService {
} }
String sysOrigin = UserCredential.parseToken(param.getToken()).getSysOrigin(); 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); sendBroadcast(param, gameListConfig);
//累计游戏排行榜 //累计游戏排行榜
incGameRankingRecord(param, gameListConfig); incGameRankingRecord(param, gameListConfig);

View File

@ -1,6 +1,9 @@
package com.red.circle.other.infra.database.cache.service.other; 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.Function;
import java.util.function.Supplier;
/** /**
* 游戏列表. * 游戏列表.
@ -19,4 +22,9 @@ public interface GameListCacheService {
*/ */
void remove(String sysOrigin); void remove(String sysOrigin);
/**
* 通过游戏ID获取游戏配置带缓存.
*/
GameListConfig getByGameIdWithCache(String gameId, String gameOrigin, String sysOrigin, Supplier<GameListConfig> orElse);
} }

View File

@ -1,9 +1,12 @@
package com.red.circle.other.infra.database.cache.service.other.impl; 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.component.redis.service.RedisService;
import com.red.circle.other.infra.database.cache.key.GameListKeys; 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.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.other.infra.utils.RedisUtils;
import com.red.circle.tool.core.json.JacksonUtils;
import com.red.circle.tool.core.text.StringUtils; import com.red.circle.tool.core.text.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -12,6 +15,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnection;
@ -53,7 +57,21 @@ public class GameListCacheServiceImpl implements GameListCacheService {
strings.forEach(redisService::delete); strings.forEach(redisService::delete);
} }
@Override
public GameListConfig getByGameIdWithCache(String gameId, String gameOrigin, String sysOrigin, Supplier<GameListConfig> 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;
}
} }