From 69329507ca5eae06837af6024b04b3485c257fef Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Thu, 11 Dec 2025 22:19:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A2=E5=AF=8C=E7=AD=89=E7=BA=A7=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/service/user/UserCacheService.java | 6 ++++ .../user/impl/UserCacheServiceImpl.java | 34 +++++++++++++------ .../user/user/UserLevelClientEndpoint.java | 4 +++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/UserCacheService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/UserCacheService.java index 4a3435b6..beb2eb00 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/UserCacheService.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/UserCacheService.java @@ -1,6 +1,7 @@ package com.red.circle.other.infra.database.cache.service.user; import com.red.circle.other.domain.model.user.NobleAbilityCO; +import com.red.circle.other.infra.database.rds.entity.user.user.ConsumptionLevel; import com.red.circle.other.inner.enums.material.PropsVipActualEquityEnum; import com.red.circle.other.infra.database.cache.entity.user.ConsumptionLevelCache; import com.red.circle.other.infra.database.cache.key.user.UserHashKey; @@ -94,4 +95,9 @@ public interface UserCacheService { */ String getSVipIdentity(Long userId, Function orElseGet); + /** + * 删除用户缓存 + */ + void removeConsumptionLevel(Long userId); + } diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/impl/UserCacheServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/impl/UserCacheServiceImpl.java index 51f8fac2..92e78a1d 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/impl/UserCacheServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/cache/service/user/impl/UserCacheServiceImpl.java @@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; import org.springframework.stereotype.Service; @@ -131,16 +132,21 @@ public class UserCacheServiceImpl implements UserCacheService { @Override public ConsumptionLevelCache getUserConsumptionLevel(Long userId, Function orElseGet) { - String key = getUserHashKey(userId); - return Optional.ofNullable(redisService.hashGet(key, UserHashKey.CONSUMPTION_LEVEL.name(), ConsumptionLevelCache.class)) - .orElseGet(() -> { - ConsumptionLevelCache levelCache = orElseGet.apply(userId); - if (Objects.nonNull(levelCache)) { - redisService - .hashPut(key, UserHashKey.CONSUMPTION_LEVEL.name(), levelCache, 1L, TimeUnit.MINUTES); - } - return levelCache; - }); + String key = getUserConsumptionKey(userId); + String cacheValue = redisService.getString(key); + return Optional.ofNullable(cacheValue) + .map(v -> JSON.parseObject(v, ConsumptionLevelCache.class)) + .orElseGet(() -> { + ConsumptionLevelCache levelCache = orElseGet.apply(userId); + if (Objects.nonNull(levelCache)) { + redisService.setString(key, JSON.toJSONString(levelCache), 1L, TimeUnit.HOURS); + } + return levelCache; + }); + } + + private static String getUserConsumptionKey(Long userId) { + return UserHashKey.CONSUMPTION_LEVEL.name() + ":" + userId; } @Override @@ -229,6 +235,14 @@ public class UserCacheServiceImpl implements UserCacheService { return vipIdentity; } + /** + * 清除用户消费等级缓存 + */ + @Override + public void removeConsumptionLevel(Long userId) { + redisService.delete(getUserConsumptionKey(userId)); + } + private String getUserHashKey(Long userId) { return UserKey.HASH.getKey(userId); } diff --git a/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/endpoint/user/user/UserLevelClientEndpoint.java b/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/endpoint/user/user/UserLevelClientEndpoint.java index f9a4387f..8a97b69d 100644 --- a/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/endpoint/user/user/UserLevelClientEndpoint.java +++ b/rc-service/rc-service-other/other-inner-endpoint/src/main/java/com/red/circle/other/app/inner/endpoint/user/user/UserLevelClientEndpoint.java @@ -4,6 +4,7 @@ import com.red.circle.common.business.core.enums.SysOriginPlatformEnum; import com.red.circle.framework.dto.ResultResponse; import com.red.circle.mq.business.model.event.task.TaskApprovalEvent; import com.red.circle.mq.rocket.business.producer.TaskMqMessage; +import com.red.circle.other.infra.database.cache.service.user.UserCacheService; import com.red.circle.other.inner.model.dto.user.WealthAndCharmExpLevelDTO; import com.red.circle.other.inner.model.dto.user.WealthAndCharmLevelDTO; import com.red.circle.other.app.inner.service.user.user.UserLevelClientService; @@ -32,6 +33,7 @@ import org.springframework.web.bind.annotation.RestController; public class UserLevelClientEndpoint implements UserLevelClientApi { private final UserLevelClientService userLevelClientService; + private final UserCacheService userCacheService; @Override public ResultResponse getUserLevelExp(SysOriginPlatformEnum sysOrigin, @@ -75,12 +77,14 @@ public class UserLevelClientEndpoint implements UserLevelClientApi { @Override public ResultResponse incrWealthExp(Long userId, BigDecimal quantity) { userLevelClientService.incrWealthExp(userId, quantity); + userCacheService.removeConsumptionLevel(userId); return ResultResponse.success(); } @Override public ResultResponse incrCharmExp(Long userId, BigDecimal quantity) { userLevelClientService.incrCharmExp(userId, quantity); + userCacheService.removeConsumptionLevel(userId); return ResultResponse.success(); }