fix: Notification 重连 + Social 修复 + Following 缓存失效

- Notification Worker 加重连循环,Channel 断开不再静默死亡
- Social: 先写 DB 再发 MQ,修复 DB 失败产生幽灵通知的问题
- Social: MQ 发布失败记日志,不再静默忽略
- 关注/取关后失效该用户的 Following Feed 缓存(24h TTL)
- Redis Client 新增 DelByPattern 按模式批量删除缓存
This commit is contained in:
yiyiis
2026-05-23 09:23:47 +08:00
parent 589116b141
commit cbfdb73fcb
3 changed files with 76 additions and 46 deletions

View File

@@ -5,16 +5,19 @@ import (
"errors"
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware/rabbitmq"
rediscache "feedsystem_video_go/internal/middleware/redis"
"log"
)
type SocialService struct {
repo *SocialRepository
accountrepo *account.AccountRepository
socialMQ *rabbitmq.SocialMQ
cache *rediscache.Client
}
func NewSocialService(repo *SocialRepository, accountrepo *account.AccountRepository, socialMQ *rabbitmq.SocialMQ) *SocialService {
return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ}
func NewSocialService(repo *SocialRepository, accountrepo *account.AccountRepository, socialMQ *rabbitmq.SocialMQ, cache *rediscache.Client) *SocialService {
return &SocialService{repo: repo, accountrepo: accountrepo, socialMQ: socialMQ, cache: cache}
}
func (s *SocialService) Follow(ctx context.Context, social *Social) error {
@@ -36,10 +39,22 @@ func (s *SocialService) Follow(ctx context.Context, social *Social) error {
if isFollowed {
return errors.New("already followed")
}
if s.socialMQ != nil {
s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID)
// 先写 DB确保数据持久化
if err := s.repo.Follow(ctx, social); err != nil {
return err
}
return s.repo.Follow(ctx, social)
// DB 成功后,失效该用户的关注列表缓存
s.invalidateFollowingFeedCache(context.Background(), social.FollowerID)
// 最后发 MQ用于通知失败只记日志不影响业务
if s.socialMQ != nil {
if err := s.socialMQ.Follow(ctx, social.FollowerID, social.VloggerID); err != nil {
log.Printf("social MQ Follow 发布失败: %v", err)
}
}
return nil
}
func (s *SocialService) Unfollow(ctx context.Context, social *Social) error {
@@ -58,10 +73,32 @@ func (s *SocialService) Unfollow(ctx context.Context, social *Social) error {
if !isFollowed {
return errors.New("not followed")
}
if s.socialMQ != nil {
s.socialMQ.UnFollow(ctx, social.FollowerID, social.VloggerID)
// 先写 DB
if err := s.repo.Unfollow(ctx, social); err != nil {
return err
}
// 失效缓存
s.invalidateFollowingFeedCache(context.Background(), social.FollowerID)
// 最后发 MQ
if s.socialMQ != nil {
if err := s.socialMQ.UnFollow(ctx, social.FollowerID, social.VloggerID); err != nil {
log.Printf("social MQ UnFollow 发布失败: %v", err)
}
}
return nil
}
func (s *SocialService) invalidateFollowingFeedCache(ctx context.Context, accountID uint) {
if s.cache == nil {
return
}
pattern := s.cache.Key("feed:listByFollowing:*:accountID=%d:*", accountID)
if err := s.cache.DelByPattern(ctx, pattern); err != nil {
log.Printf("失效 Following 缓存失败: accountID=%d, err=%v", accountID, err)
}
return s.repo.Unfollow(ctx, social)
}
func (s *SocialService) GetAllFollowers(ctx context.Context, VloggerID uint) ([]*account.Account, error) {