feat(task): 添加视频生成通知防重机制- 新增Redis缓存键VIDEO_NOTIFICATION_CACHE_KEY用于记录通知发送状态

- 设置通知发送间隔为2分钟,防止重复发送
- 在发送通知前检查缓存,若3分钟内已发送则跳过- 发送成功后更新Redis缓存并设置过期时间
- 添加相关日志记录以方便追踪通知发送情况
This commit is contained in:
2025-10-15 18:43:54 +08:00
parent 645afbaf0c
commit d5cd1924f5

View File

@@ -81,7 +81,9 @@ import java.util.stream.Collectors;
@Service
public class TaskTaskServiceImpl implements TaskService {
private static final String WORKER_SELF_HOSTED_CACHE_KEY = "worker_self_hosted_scenic:%s";
private static final String VIDEO_NOTIFICATION_CACHE_KEY = "video_notification_member:%s";
private static final int CACHE_EXPIRE_MINUTES = 3;
private static final int NOTIFICATION_CACHE_EXPIRE_MINUTES = 2;
@Autowired
private TaskMapper taskMapper;
@Autowired
@@ -621,6 +623,15 @@ public class TaskTaskServiceImpl implements TaskService {
@Override
public void sendVideoGeneratedServiceNotification(Long taskId, Long memberId) {
// 检查Redis中该memberId是否在3分钟内已发送过通知
String notificationCacheKey = String.format(VIDEO_NOTIFICATION_CACHE_KEY, memberId);
String cachedValue = redisTemplate.opsForValue().get(notificationCacheKey);
if (cachedValue != null) {
log.info("memberId:{} 在3分钟内已发送过通知,跳过本次发送", memberId);
return;
}
MemberVideoEntity item = videoMapper.queryRelationByMemberTask(memberId, taskId);
MemberRespVO member = memberMapper.getById(memberId);
String openId = member.getOpenId();
@@ -673,6 +684,10 @@ public class TaskTaskServiceImpl implements TaskService {
msg.setSendReason("视频生成通知");
msg.setSendBiz("视频生成");
ztMessageProducerService.send(msg);
// 发送成功后,设置Redis缓存,2分钟过期
redisTemplate.opsForValue().set(notificationCacheKey, String.valueOf(System.currentTimeMillis()), NOTIFICATION_CACHE_EXPIRE_MINUTES, TimeUnit.MINUTES);
log.debug("memberId:{} 通知发送成功,已设置{}分钟缓存", memberId, NOTIFICATION_CACHE_EXPIRE_MINUTES);
}
}