cpList 缓存重写,定时任务更改

This commit is contained in:
tianfeng 2025-12-12 11:36:06 +08:00
parent d0936e24f9
commit 51c5cf0712
11 changed files with 185 additions and 88 deletions

View File

@ -6,6 +6,7 @@ import com.red.circle.other.app.dto.cmd.user.relation.cp.CpApplyDismissCmd;
import com.red.circle.other.app.service.user.user.UserProfileService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.common.user.UserGiftBackpackCommon;
import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService;
import com.red.circle.other.infra.database.cache.service.user.UserCpValueCacheService;
import com.red.circle.other.infra.database.mongo.service.user.count.WeekCpValueCountService;
import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship;
@ -40,6 +41,7 @@ public class DismissCpApplyCmdExe {
private final CpBlessRecordService cpBlessRecordService;
private final CpCabinAchieveService cpCabinAchieveService;
private final UserProfileGateway userProfileGateway;
private final CpRelationshipCacheService cpRelationshipCacheService;
public void execute(CpApplyDismissCmd cmd) {
@ -54,7 +56,7 @@ public class DismissCpApplyCmdExe {
// 更新CP关系为分手中不删除数据
cpRelationshipService.updateToDismissing(cmd.requiredReqUserId(), cmd.getCpUserId());
userProfileGateway.removeCacheAll(Arrays.asList(cmd.requiredReqUserId(), cmd.getCpUserId()));
cpRelationshipCacheService.remove(Arrays.asList(cmd.requiredReqUserId(), cmd.getCpUserId()));
// 以下数据不删除7天后由定时任务清理
// weekCpValueCountService.removeThisWeek(...)

View File

@ -8,6 +8,7 @@ import com.red.circle.mq.business.model.event.user.CpApplyEvent;
import com.red.circle.mq.rocket.business.producer.UserMqMessageService;
import com.red.circle.other.app.dto.cmd.user.relation.cp.ProcessApplyCmd;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService;
import com.red.circle.other.infra.database.rds.entity.user.user.CpApply;
import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship;
import com.red.circle.other.infra.database.rds.entity.user.user.CpValue;
@ -44,6 +45,7 @@ public class ProcessCpApplyCmd {
private final UserMqMessageService userMqMessageService;
private final CpRelationshipService cpRelationshipService;
private final UserProfileGateway userProfileGateway;
private final CpRelationshipCacheService cpRelationshipCacheService;
public void execute(ProcessApplyCmd cmd) {
process(cmd);
@ -96,7 +98,7 @@ public class ProcessCpApplyCmd {
cmd.requireReqSysOrigin());
}
userProfileGateway.removeCacheAll(Arrays.asList(cpApply.getSendApplyUserId(), cpApply.getAcceptApplyUserId()));
cpRelationshipCacheService.remove(Arrays.asList(cpApply.getSendApplyUserId(), cpApply.getAcceptApplyUserId()));
}
private void refuseStatusWait(ProcessApplyCmd cmd, CpApply cpApply) {

View File

@ -14,6 +14,8 @@ import com.red.circle.other.app.service.user.relation.UserSupportRelationService
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.cache.key.user.UserKey;
import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService;
import com.red.circle.other.infra.database.mongo.entity.live.RoomProfileManager;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.rds.service.user.user.CpValueService;
@ -51,6 +53,7 @@ public class UserProfileUseQryExe {
private final RedisService redisService;
private final UserSupportRelationService userRelationInterviewService;
private final CpValueService cpValueService;
private final CpRelationshipCacheService cpRelationshipCacheService;
public UserProfileDTO execute(UserProfileQryCmd cmd) {
UserProfile userProfile = getUserProfile(cmd);
@ -96,18 +99,9 @@ public class UserProfileUseQryExe {
userRelationInterviewService.addVisitor(userIdCmd);
}
// 重新赋值cpValue
if (CollectionUtil.isNotEmpty(userProfileDTO.getCpList())) {
Set<Long> cpValIdSet = userProfileDTO.getCpList().stream().map(CpSimpleUserProfileCO::getCpValId).collect(Collectors.toSet());
Map<Long, BigDecimal> mapCpVal = cpValueService.mapCpVal(cpValIdSet);
userProfileDTO.getCpList().forEach(cp -> {
BigDecimal cpValue = mapCpVal.get(cp.getCpValId());
if (Objects.nonNull(cpValue)) {
cp.setCpValue(cpValue);
}
});
}
// 填充用户CP信息
userProfileDTO.setCpList(cpRelationshipCacheService.getUserCpList(userProfile.getId()));
return userProfileDTO;
}

View File

@ -13,6 +13,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.common.game.GameKtvCommon;
import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService;
import com.red.circle.other.infra.database.cache.service.user.UserCpValueCacheService;
import com.red.circle.other.infra.database.mongo.dto.TemporaryActivityCountParam;
import com.red.circle.other.infra.database.mongo.entity.gift.GiftAcceptUser;
@ -79,6 +80,7 @@ public class RankCountStrategy implements GiftStrategy {
private final GiftGiveRunningWaterService giftGiveRunningWaterService;
private final TemporaryActivityCountService temporaryActivityCountService;
private final RankingActivityService rankingActivityService;
private final CpRelationshipCacheService cpRelationshipCacheService;
@Override
public void processor(OfflineProcessGiftEvent event) {
@ -340,8 +342,9 @@ public class RankCountStrategy implements GiftStrategy {
userCpValueCacheService.incr(cp.getUserId(),
cp.getCpUserId(),
acceptAmount.longValue());
});
cpRelationshipCacheService.remove(Arrays.asList(cp.getUserId(), cp.getCpUserId()));
});
// 更新cp用户赠送的礼物价值
// cpRelationshipService.updateAccountByUserId(cpRelationship.getUserId(),
// cpRelationship.getAmount().add(acceptAmount));

View File

@ -1,7 +1,9 @@
package com.red.circle.other.app.scheduler;
import com.google.common.collect.Sets;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.common.user.UserGiftBackpackCommon;
import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService;
import com.red.circle.other.infra.database.cache.service.user.UserCpValueCacheService;
import com.red.circle.other.infra.database.mongo.service.user.count.WeekCpValueCountService;
import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship;
@ -10,6 +12,8 @@ import com.red.circle.other.infra.database.rds.service.user.user.CpCabinAchieveS
import com.red.circle.other.infra.database.rds.service.user.user.CpRelationshipService;
import com.red.circle.other.infra.database.rds.service.user.user.CpValueService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.Arrays;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -33,6 +37,8 @@ public class CleanExpiredDismissingCpTask {
private final UserCpValueCacheService userCpValueCacheService;
private final CpCabinAchieveService cpCabinAchieveService;
private final CpBlessRecordService cpBlessRecordService;
private final UserProfileGateway userProfileGateway;
private final CpRelationshipCacheService cpRelationshipCacheService;
/**
* 每天凌晨3点执行清理任务.
@ -58,7 +64,7 @@ public class CleanExpiredDismissingCpTask {
private void cleanCpData(CpRelationship cpRelationship) {
try {
// 删除CP关系
cpRelationshipService.deleteCpRelationship(cpRelationship.getUserId());
cpRelationshipService.deleteById(cpRelationship.getId());
// 删除周统计
weekCpValueCountService.removeThisWeek(cpRelationship.getUserId(),
@ -80,6 +86,8 @@ public class CleanExpiredDismissingCpTask {
// 删除祝福记录
cpBlessRecordService.deleteByCpValId(cpRelationship.getCpValId());
cpRelationshipCacheService.remove(Arrays.asList(cpRelationship.getUserId(), cpRelationship.getCpUserId()));
} catch (Exception e) {
log.error("CleanExpiredDismissingCpTask cleanCpData error, userId={}, cpUserId={}",
cpRelationship.getUserId(), cpRelationship.getCpUserId(), e);

View File

@ -62,7 +62,13 @@ public enum UserKey implements RedisKeys {
/**
* 系统用户友谊卡配置.
*/
FRIENDSHIP_CARD_CONF;
FRIENDSHIP_CARD_CONF,
/**
* 用户CP
*/
CP,
;
@Override
public String businessPrefix() {

View File

@ -0,0 +1,24 @@
package com.red.circle.other.infra.database.cache.service.cp;
import com.red.circle.other.inner.model.dto.user.CpSimpleUserProfileCO;
import java.util.List;
import java.util.function.Function;
/**
* CP关系缓存服务.
*/
public interface CpRelationshipCacheService {
/**
* 获取用户CP列表.
*/
List<CpSimpleUserProfileCO> getUserCpList(Long userId);
/**
* 移除用户CP列表缓存.
*
* @param userId 用户id
*/
void remove(List<Long> userId);
}

View File

@ -0,0 +1,126 @@
package com.red.circle.other.infra.database.cache.service.cp.impl;
import com.alibaba.fastjson.JSON;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.other.infra.database.cache.key.user.UserKey;
import com.red.circle.other.infra.database.cache.service.cp.CpRelationshipCacheService;
import com.red.circle.other.infra.database.rds.entity.user.user.BaseInfo;
import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship;
import com.red.circle.other.infra.database.rds.service.user.user.BaseInfoService;
import com.red.circle.other.infra.database.rds.service.user.user.CpRelationshipService;
import com.red.circle.other.infra.database.rds.service.user.user.CpValueService;
import com.red.circle.other.inner.model.dto.user.CpSimpleUserProfileCO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* CP关系缓存服务实现.
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class CpRelationshipCacheServiceImpl implements CpRelationshipCacheService {
private final CpRelationshipService cpRelationshipService;
private final RedisService redisService;
private final BaseInfoService baseInfoService;
private final CpValueService cpValueService;
private static final long CACHE_EXPIRE_SECONDS = 7200L;
@Override
public List<CpSimpleUserProfileCO> getUserCpList(Long userId) {
String key = getKey(userId);
String cacheData = redisService.getString(key);
if (cacheData != null) {
return JSON.parseArray(cacheData, CpSimpleUserProfileCO.class);
}
try {
List<CpRelationship> cpRelationshipList = cpRelationshipService.getByUserId(userId);
if (CollectionUtils.isEmpty(cpRelationshipList)) {
redisService.setString(key, JSON.toJSONString(Collections.emptyList()), CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS);
return Collections.emptyList();
}
// 批量获取CP用户信息
Set<Long> cpUserIds = cpRelationshipList.stream()
.map(CpRelationship::getCpUserId)
.collect(Collectors.toSet());
List<BaseInfo> cpUserProfiles = baseInfoService.listByUserId(cpUserIds);
Map<Long, BaseInfo> cpUserProfileMap = cpUserProfiles.stream()
.collect(Collectors.toMap(BaseInfo::getId, Function.identity()));
BaseInfo baseInfo = baseInfoService.getById(userId);
// 批量获取CP值
Set<Long> cpValIds = cpRelationshipList.stream()
.map(CpRelationship::getCpValId)
.collect(Collectors.toSet());
Map<Long, BigDecimal> cpValueMap = cpValueService.mapCpVal(cpValIds);
// 组装CP列表
List<CpSimpleUserProfileCO> cpList = cpRelationshipList.stream()
.map(cpRelationship -> {
BaseInfo cpUser = cpUserProfileMap.get(cpRelationship.getCpUserId());
if (cpUser == null) {
return null;
}
BigDecimal cpValue = cpValueMap.getOrDefault(cpRelationship.getCpValId(), BigDecimal.ZERO);
Long days = DateUtils.toDurationDays(cpRelationship.getCreateTime(), DateUtils.now());
String firstDay = cpRelationship.getCreateTime().toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate().toString();
return new CpSimpleUserProfileCO()
.setMeUserId(baseInfo.getId())
.setMeAccount(baseInfo.getAccount())
.setMeUserAvatar(baseInfo.getUserAvatar())
.setMeUserNickname(baseInfo.getUserNickname())
.setCpUserId(cpUser.getId())
.setCpAccount(cpUser.getAccount())
.setCpUserAvatar(cpUser.getUserAvatar())
.setCpUserNickname(cpUser.getUserNickname())
.setCpValId(cpRelationship.getCpValId())
.setCpValue(cpValue)
.setCreateTime(cpRelationship.getCreateTime())
.setDays(days)
.setFirstDay(firstDay);
})
.filter(Objects::nonNull)
.sorted(Comparator.comparing(CpSimpleUserProfileCO::getCpValue).reversed()
.thenComparing(CpSimpleUserProfileCO::getCreateTime))
.toList();
redisService.setString(key, JSON.toJSONString(cpList), CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS);
return cpList;
} catch (Exception e) {
log.error("填充CP数据失败, userId={}", userId, e);
}
return Collections.emptyList();
}
private static String getKey(Long userId) {
return UserKey.CP.getKey(userId);
}
@Override
public void remove(List<Long> userIds) {
userIds.forEach(userId -> {
redisService.delete(getKey(userId));
});
}
}

View File

@ -63,11 +63,9 @@ public interface CpRelationshipService extends BaseService<CpRelationship> {
Long getCpValByUserId(Long userId);
/**
* 删除cp关系.
*
* @param userId 用户id
* 根据id删除
*/
void deleteCpRelationship(Long userId);
void deleteById(Long id);
/**
* 组建cp.

View File

@ -114,9 +114,8 @@ public class CpRelationshipServiceImpl extends
}
@Override
public void deleteCpRelationship(Long userId) {
delete().eq(CpRelationship::getCpUserId, userId).last(PageConstant.LIMIT_ONE).execute();
delete().eq(CpRelationship::getUserId, userId).last(PageConstant.LIMIT_ONE).execute();
public void deleteById(Long id) {
removeById(id);
}
@Override

View File

@ -283,9 +283,6 @@ public class UserRunProfileTransportGatewayImpl implements UserRunProfileTranspo
// 用户是否首充处理
fillUserFirstRecharge(newUserRunProfile);
// 填充CP列表数据
fillUserCpList(newUserRunProfile);
newUserRunProfile.addUseProps(userUsePropsEntityMap.get(userBaseInfo.getId()));
newUserRunProfile.addUseProps(userUseRoomTheme.get(userBaseInfo.getId()));
newUserRunProfile.setOwnSpecialId(userSpecialIdMap.get(userBaseInfo.getId()));
@ -322,68 +319,6 @@ public class UserRunProfileTransportGatewayImpl implements UserRunProfileTranspo
}
}
/**
* 填充用户CP列表数据
*/
private void fillUserCpList(UserRunProfile userRunProfile) {
try {
List<CpRelationship> cpRelationshipList = cpRelationshipService.getByUserId(userRunProfile.getId());
if (CollectionUtils.isEmpty(cpRelationshipList)) {
return;
}
// 批量获取CP用户信息
Set<Long> cpUserIds = cpRelationshipList.stream()
.map(CpRelationship::getCpUserId)
.collect(Collectors.toSet());
List<BaseInfo> cpUserProfiles = baseInfoService.listByUserId(cpUserIds);
Map<Long, BaseInfo> cpUserProfileMap = cpUserProfiles.stream()
.collect(Collectors.toMap(BaseInfo::getId, Function.identity()));
// 批量获取CP值
Set<Long> cpValIds = cpRelationshipList.stream()
.map(CpRelationship::getCpValId)
.collect(Collectors.toSet());
Map<Long, BigDecimal> cpValueMap = cpValueService.mapCpVal(cpValIds);
// 组装CP列表
List<CpSimpleUserProfileCO> cpList = cpRelationshipList.stream()
.map(cpRelationship -> {
BaseInfo cpUser = cpUserProfileMap.get(cpRelationship.getCpUserId());
if (cpUser == null) {
return null;
}
BigDecimal cpValue = cpValueMap.getOrDefault(cpRelationship.getCpValId(), BigDecimal.ZERO);
Long days = DateUtils.toDurationDays(cpRelationship.getCreateTime(), DateUtils.now());
String firstDay = cpRelationship.getCreateTime().toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate().toString();
return new CpSimpleUserProfileCO()
.setMeUserId(userRunProfile.getId())
.setMeAccount(userRunProfile.getAccount())
.setMeUserAvatar(userRunProfile.getUserAvatar())
.setMeUserNickname(userRunProfile.getUserNickname())
.setCpUserId(cpUser.getId())
.setCpAccount(cpUser.getAccount())
.setCpUserAvatar(cpUser.getUserAvatar())
.setCpUserNickname(cpUser.getUserNickname())
.setCpValId(cpRelationship.getCpValId())
.setCpValue(cpValue)
.setCreateTime(cpRelationship.getCreateTime())
.setDays(days)
.setFirstDay(firstDay);
})
.filter(Objects::nonNull)
.sorted(Comparator.comparing(CpSimpleUserProfileCO::getCpValue).reversed()
.thenComparing(CpSimpleUserProfileCO::getCreateTime))
.toList();
userRunProfile.setCpList(cpList);
} catch (Exception e) {
log.error("填充CP数据失败, userId={}", userRunProfile.getId(), e);
}
}
private Map<Long, List<UseBadgeDTO>> mapUserUseBadgeEntity(String sysOrigin,
Set<Long> userIds) {
return Optional.ofNullable(this.listUserUseBadgeEntity(sysOrigin, userIds))