财富等级缓存处理

This commit is contained in:
tianfeng 2025-12-11 22:19:21 +08:00
parent 174557924d
commit 69329507ca
3 changed files with 34 additions and 10 deletions

View File

@ -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<Long, String> orElseGet);
/**
* 删除用户缓存
*/
void removeConsumptionLevel(Long userId);
}

View File

@ -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<Long, ConsumptionLevelCache> 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);
}

View File

@ -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<WealthAndCharmExpLevelDTO> getUserLevelExp(SysOriginPlatformEnum sysOrigin,
@ -75,12 +77,14 @@ public class UserLevelClientEndpoint implements UserLevelClientApi {
@Override
public ResultResponse<Void> incrWealthExp(Long userId, BigDecimal quantity) {
userLevelClientService.incrWealthExp(userId, quantity);
userCacheService.removeConsumptionLevel(userId);
return ResultResponse.success();
}
@Override
public ResultResponse<Void> incrCharmExp(Long userId, BigDecimal quantity) {
userLevelClientService.incrCharmExp(userId, quantity);
userCacheService.removeConsumptionLevel(userId);
return ResultResponse.success();
}