From 48c885da67376615e34c33721e1c6bb3ac9896b2 Mon Sep 17 00:00:00 2001 From: yang hao <1013055366@qq.com> Date: Tue, 24 Oct 2023 13:21:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E7=B2=89=E4=B8=9D=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/follow/rpc/follow.proto | 6 +- .../rpc/internal/logic/fanslistlogic.go | 168 +++++++++++++++++- .../follow/rpc/internal/model/follow.go | 10 ++ application/follow/rpc/pb/follow.pb.go | 107 +++++++---- application/follow/rpc/pb/follow_grpc.pb.go | 2 +- 5 files changed, 255 insertions(+), 38 deletions(-) diff --git a/application/follow/rpc/follow.proto b/application/follow/rpc/follow.proto index dab9c96..e21df15 100644 --- a/application/follow/rpc/follow.proto +++ b/application/follow/rpc/follow.proto @@ -60,10 +60,14 @@ message FansListRequest { message FansItem { int64 userId = 1; int64 fansUserId = 2; - int64 createTime = 3; + int64 followCount = 3; + int64 fansCount = 4; + int64 createTime = 5; } message FansListResponse { repeated FansItem items = 1; int64 cursor = 2; + bool isEnd = 3; + int64 Id = 4; } \ No newline at end of file diff --git a/application/follow/rpc/internal/logic/fanslistlogic.go b/application/follow/rpc/internal/logic/fanslistlogic.go index d5c424e..66da080 100644 --- a/application/follow/rpc/internal/logic/fanslistlogic.go +++ b/application/follow/rpc/internal/logic/fanslistlogic.go @@ -2,13 +2,21 @@ package logic import ( "context" + "strconv" + "time" + "beyond/application/follow/code" + "beyond/application/follow/rpc/internal/model" "beyond/application/follow/rpc/internal/svc" + "beyond/application/follow/rpc/internal/types" "beyond/application/follow/rpc/pb" "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/core/threading" ) +const userFansExpireTime = 3600 * 24 * 2 + type FansListLogic struct { ctx context.Context svcCtx *svc.ServiceContext @@ -25,7 +33,163 @@ func NewFansListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FansList // FansList 粉丝列表. func (l *FansListLogic) FansList(in *pb.FansListRequest) (*pb.FansListResponse, error) { - // todo: add your logic here and delete this line + if in.UserId == 0 { + return nil, code.UserIdEmpty + } + if in.PageSize == 0 { + in.PageSize = types.DefaultPageSize + } + if in.Cursor == 0 { + in.Cursor = time.Now().Unix() + } + var ( + err error + isCache, isEnd bool + lastId, cursor int64 + fansUserIds []int64 + fanss []*model.Follow + curPage []*pb.FansItem + ) + FansUIds, CreateTime, _ := l.cacheFansUserIds(l.ctx, in.UserId, in.Cursor, in.PageSize) + if len(FansUIds) > 0 { + isCache = true + if FansUIds[len(FansUIds)-1] == -1 { + FansUIds = FansUIds[:len(FansUIds)-1] + isEnd = true + } + if len(FansUIds) == 0 { + return &pb.FansListResponse{}, nil + } + fansUserIds = FansUIds + for i, fansUId := range FansUIds { + curPage = append(curPage, &pb.FansItem{ + UserId: in.UserId, + FansUserId: fansUId, + CreateTime: CreateTime[i], + }) + } + } else { + fanss, err = l.svcCtx.FollowModel.FindByFollowedUserId(l.ctx, in.UserId, types.CacheMaxFansCount) + if err != nil { + l.Logger.Errorf("[FansList] FollowModel.FindByFollowedUserId error: %v req: %v", err, in) + return nil, err + } + if len(fanss) == 0 { + return &pb.FansListResponse{}, nil + } + var firstPageFans []*model.Follow + if len(fanss) > int(in.PageSize) { + firstPageFans = fanss[:in.PageSize] + } else { + firstPageFans = fanss + isEnd = true + } + for _, fans := range firstPageFans { + fansUserIds = append(fansUserIds, fans.UserID) + curPage = append(curPage, &pb.FansItem{ + UserId: fans.FollowedUserID, + FansUserId: fans.UserID, + CreateTime: fans.CreateTime.Unix(), + }) + } + } + if len(curPage) > 0 { + pageLast := curPage[len(curPage)-1] + lastId = pageLast.FansUserId + cursor = pageLast.CreateTime + if cursor < 0 { + cursor = 0 + } + for i, fans := range curPage { + if fans.CreateTime == in.Cursor { + curPage = curPage[i:] + break + } + } + } + fa, err := l.svcCtx.FollowCountModel.FindByUserIds(l.ctx, fansUserIds) + if err != nil { + l.Logger.Errorf("[FansList] FollowCountModel.FindByUserIds error: %v fansUserIds: %v", err, fansUserIds) + } + uidFansCount := make(map[int64]int) + uidFollowCount := make(map[int64]int) + for _, f := range fa { + uidFansCount[f.UserID] = f.FansCount + uidFollowCount[f.UserID] = f.FollowCount + } + for _, cur := range curPage { + cur.FansCount = int64(uidFansCount[cur.FansUserId]) + cur.FollowCount = int64(uidFollowCount[cur.FansUserId]) + } + + ret := &pb.FansListResponse{ + Items: curPage, + Cursor: cursor, + IsEnd: isEnd, + Id: lastId, + } + + if !isCache { + threading.GoSafe(func() { + if len(fanss) < types.CacheMaxFansCount && len(fanss) > 0 { + fanss = append(fanss, &model.Follow{UserID: -1}) + } + err = l.addCacheFans(context.Background(), in.UserId, fanss) + }) + } + return ret, nil +} + +func (l *FansListLogic) cacheFansUserIds(ctx context.Context, userId, cursor, pageSize int64) ([]int64, []int64, error) { + key := userFansKey(userId) + b, err := l.svcCtx.BizRedis.ExistsCtx(ctx, key) + if err != nil { + logx.Errorf("[cacheFansUserIds] BizRedis.ExistsCtx error: %v", err) + } + if b { + err = l.svcCtx.BizRedis.ExpireCtx(ctx, key, userFansExpireTime) + if err != nil { + logx.Errorf("[cacheFansUserIds] BizRedis.ExpireCtx error: %v", err) + } + } + pairs, err := l.svcCtx.BizRedis.ZrevrangebyscoreWithScoresAndLimitCtx(ctx, key, 0, cursor, 0, int(pageSize)) + if err != nil { + logx.Errorf("[cacheFansUserIds] BizRedis.ZrevrangebyscoreWithScoresAndLimitCtx error: %v", err) + return nil, nil, err + } + var uids []int64 + var createTimes []int64 + for _, pair := range pairs { + uid, err := strconv.ParseInt(pair.Key, 10, 64) + createTime, err := strconv.ParseInt(strconv.FormatInt(pair.Score, 10), 10, 64) + if err != nil { + logx.Errorf("[cacheFansUserIds] strconv.ParseInt error: %v", err) + continue + } + uids = append(uids, uid) + createTimes = append(createTimes, createTime) + } + return uids, createTimes, nil +} + +func (l *FansListLogic) addCacheFans(ctx context.Context, userId int64, fans []*model.Follow) error { + if len(fans) == 0 { + return nil + } + key := userFansKey(userId) + for _, fan := range fans { + var score int64 + if fan.UserID == -1 { + score = 0 + } else { + score = fan.CreateTime.Unix() + } + _, err := l.svcCtx.BizRedis.ZaddCtx(ctx, key, score, strconv.FormatInt(fan.UserID, 10)) + if err != nil { + logx.Errorf("[addCacheFans] BizRedis.ZaddCtx error: %v", err) + return err + } + } - return &pb.FansListResponse{}, nil + return l.svcCtx.BizRedis.ExpireCtx(ctx, key, userFollowExpireTime) } diff --git a/application/follow/rpc/internal/model/follow.go b/application/follow/rpc/internal/model/follow.go index c9eaa6b..8481e0c 100644 --- a/application/follow/rpc/internal/model/follow.go +++ b/application/follow/rpc/internal/model/follow.go @@ -79,3 +79,13 @@ func (m *FollowModel) FindByFollowedUserIds(ctx context.Context, followedUserIds return result, err } + +func (m *FollowModel) FindByFollowedUserId(ctx context.Context, userId int64, limit int) ([]*Follow, error) { + var result []*Follow + err := m.db.WithContext(ctx). + Where("followed_user_id = ? AND follow_status = ?", userId, 1). + Order("id desc"). + Limit(limit). + Find(&result).Error + return result, err +} diff --git a/application/follow/rpc/pb/follow.pb.go b/application/follow/rpc/pb/follow.pb.go index 9f09633..be1f54d 100644 --- a/application/follow/rpc/pb/follow.pb.go +++ b/application/follow/rpc/pb/follow.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.28.0 -// protoc v3.19.1 +// protoc v4.24.3 // source: follow.proto package pb @@ -487,9 +487,11 @@ type FansItem struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - UserId int64 `protobuf:"varint,1,opt,name=userId,proto3" json:"userId,omitempty"` - FansUserId int64 `protobuf:"varint,2,opt,name=fansUserId,proto3" json:"fansUserId,omitempty"` - CreateTime int64 `protobuf:"varint,3,opt,name=createTime,proto3" json:"createTime,omitempty"` + UserId int64 `protobuf:"varint,1,opt,name=userId,proto3" json:"userId,omitempty"` + FansUserId int64 `protobuf:"varint,2,opt,name=fansUserId,proto3" json:"fansUserId,omitempty"` + FollowCount int64 `protobuf:"varint,3,opt,name=followCount,proto3" json:"followCount,omitempty"` + FansCount int64 `protobuf:"varint,4,opt,name=fansCount,proto3" json:"fansCount,omitempty"` + CreateTime int64 `protobuf:"varint,5,opt,name=createTime,proto3" json:"createTime,omitempty"` } func (x *FansItem) Reset() { @@ -538,6 +540,20 @@ func (x *FansItem) GetFansUserId() int64 { return 0 } +func (x *FansItem) GetFollowCount() int64 { + if x != nil { + return x.FollowCount + } + return 0 +} + +func (x *FansItem) GetFansCount() int64 { + if x != nil { + return x.FansCount + } + return 0 +} + func (x *FansItem) GetCreateTime() int64 { if x != nil { return x.CreateTime @@ -552,6 +568,8 @@ type FansListResponse struct { Items []*FansItem `protobuf:"bytes,1,rep,name=items,proto3" json:"items,omitempty"` Cursor int64 `protobuf:"varint,2,opt,name=cursor,proto3" json:"cursor,omitempty"` + IsEnd bool `protobuf:"varint,3,opt,name=isEnd,proto3" json:"isEnd,omitempty"` + Id int64 `protobuf:"varint,4,opt,name=Id,proto3" json:"Id,omitempty"` } func (x *FansListResponse) Reset() { @@ -600,6 +618,20 @@ func (x *FansListResponse) GetCursor() int64 { return 0 } +func (x *FansListResponse) GetIsEnd() bool { + if x != nil { + return x.IsEnd + } + return false +} + +func (x *FansListResponse) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + var File_follow_proto protoreflect.FileDescriptor var file_follow_proto_rawDesc = []byte{ @@ -646,36 +678,43 @@ var file_follow_proto_rawDesc = []byte{ 0x64, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x70, 0x61, 0x67, - 0x65, 0x53, 0x69, 0x7a, 0x65, 0x22, 0x62, 0x0a, 0x08, 0x46, 0x61, 0x6e, 0x73, 0x49, 0x74, 0x65, - 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x03, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1e, 0x0a, 0x0a, 0x66, 0x61, 0x6e, - 0x73, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x66, - 0x61, 0x6e, 0x73, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1e, 0x0a, 0x0a, 0x63, 0x72, 0x65, - 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x63, - 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x22, 0x53, 0x0a, 0x10, 0x46, 0x61, 0x6e, - 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x27, 0x0a, - 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x73, - 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x61, 0x6e, 0x73, 0x49, 0x74, 0x65, 0x6d, 0x52, - 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x32, 0x8c, - 0x02, 0x0a, 0x06, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x12, 0x39, 0x0a, 0x06, 0x46, 0x6f, 0x6c, - 0x6c, 0x6f, 0x77, 0x12, 0x16, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x6f, - 0x6c, 0x6c, 0x6f, 0x77, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x73, 0x65, - 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3f, 0x0a, 0x08, 0x55, 0x6e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, - 0x12, 0x18, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x6e, 0x46, 0x6f, 0x6c, - 0x6c, 0x6f, 0x77, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x73, 0x65, 0x72, - 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x6e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x45, 0x0a, 0x0a, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x4c, - 0x69, 0x73, 0x74, 0x12, 0x1a, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x6f, - 0x6c, 0x6c, 0x6f, 0x77, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, - 0x1b, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, - 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3f, 0x0a, 0x08, - 0x46, 0x61, 0x6e, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x18, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x2e, 0x46, 0x61, 0x6e, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, - 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x61, 0x6e, - 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x06, 0x5a, - 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x53, 0x69, 0x7a, 0x65, 0x22, 0xa2, 0x01, 0x0a, 0x08, 0x46, 0x61, 0x6e, 0x73, 0x49, 0x74, + 0x65, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1e, 0x0a, 0x0a, 0x66, 0x61, + 0x6e, 0x73, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, + 0x66, 0x61, 0x6e, 0x73, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x0b, 0x66, 0x6f, + 0x6c, 0x6c, 0x6f, 0x77, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x0b, 0x66, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1c, 0x0a, 0x09, + 0x66, 0x61, 0x6e, 0x73, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x09, 0x66, 0x61, 0x6e, 0x73, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1e, 0x0a, 0x0a, 0x63, 0x72, + 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, + 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x22, 0x79, 0x0a, 0x10, 0x46, 0x61, + 0x6e, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x27, + 0x0a, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, + 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x61, 0x6e, 0x73, 0x49, 0x74, 0x65, 0x6d, + 0x52, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, + 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x12, + 0x14, 0x0a, 0x05, 0x69, 0x73, 0x45, 0x6e, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, + 0x69, 0x73, 0x45, 0x6e, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x02, 0x49, 0x64, 0x32, 0x8c, 0x02, 0x0a, 0x06, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, + 0x12, 0x39, 0x0a, 0x06, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x12, 0x16, 0x2e, 0x73, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x6f, 0x6c, + 0x6c, 0x6f, 0x77, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x3f, 0x0a, 0x08, 0x55, + 0x6e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x12, 0x18, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x2e, 0x55, 0x6e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x19, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x55, 0x6e, 0x46, 0x6f, + 0x6c, 0x6c, 0x6f, 0x77, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x45, 0x0a, 0x0a, + 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x1a, 0x2e, 0x73, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x4c, 0x69, 0x73, 0x74, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x2e, 0x46, 0x6f, 0x6c, 0x6c, 0x6f, 0x77, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x3f, 0x0a, 0x08, 0x46, 0x61, 0x6e, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x12, + 0x18, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x46, 0x61, 0x6e, 0x73, 0x4c, 0x69, + 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x73, 0x65, 0x72, 0x76, + 0x69, 0x63, 0x65, 0x2e, 0x46, 0x61, 0x6e, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/application/follow/rpc/pb/follow_grpc.pb.go b/application/follow/rpc/pb/follow_grpc.pb.go index 1bb9b7e..f20c601 100644 --- a/application/follow/rpc/pb/follow_grpc.pb.go +++ b/application/follow/rpc/pb/follow_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.2.0 -// - protoc v3.19.1 +// - protoc v4.24.3 // source: follow.proto package pb From 7307b742e17a202403862dd7b2b1aa6783dc90fb Mon Sep 17 00:00:00 2001 From: yang hao <1013055366@qq.com> Date: Thu, 26 Oct 2023 09:57:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E4=BB=A5=E5=8F=8A=E5=91=BD=E5=90=8D=E8=A7=84?= =?UTF-8?q?=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/follow/rpc/follow.proto | 1 + .../rpc/internal/logic/fanslistlogic.go | 36 +++++++++---------- application/follow/rpc/pb/follow.pb.go | 13 +++++-- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/application/follow/rpc/follow.proto b/application/follow/rpc/follow.proto index e21df15..0de2b29 100644 --- a/application/follow/rpc/follow.proto +++ b/application/follow/rpc/follow.proto @@ -55,6 +55,7 @@ message FansListRequest { int64 userId = 1; int64 cursor = 2; int64 pageSize = 3; + int64 Id = 4; } message FansItem { diff --git a/application/follow/rpc/internal/logic/fanslistlogic.go b/application/follow/rpc/internal/logic/fanslistlogic.go index 66da080..af05c5b 100644 --- a/application/follow/rpc/internal/logic/fanslistlogic.go +++ b/application/follow/rpc/internal/logic/fanslistlogic.go @@ -47,41 +47,41 @@ func (l *FansListLogic) FansList(in *pb.FansListRequest) (*pb.FansListResponse, isCache, isEnd bool lastId, cursor int64 fansUserIds []int64 - fanss []*model.Follow + fansModel []*model.Follow curPage []*pb.FansItem ) - FansUIds, CreateTime, _ := l.cacheFansUserIds(l.ctx, in.UserId, in.Cursor, in.PageSize) - if len(FansUIds) > 0 { + fansUIds, createTime, _ := l.cacheFansUserIds(l.ctx, in.UserId, in.Cursor, in.PageSize) + if len(fansUIds) > 0 { isCache = true - if FansUIds[len(FansUIds)-1] == -1 { - FansUIds = FansUIds[:len(FansUIds)-1] + if fansUIds[len(fansUIds)-1] == -1 { + fansUIds = fansUIds[:len(fansUIds)-1] isEnd = true } - if len(FansUIds) == 0 { + if len(fansUIds) == 0 { return &pb.FansListResponse{}, nil } - fansUserIds = FansUIds - for i, fansUId := range FansUIds { + fansUserIds = fansUIds + for i, fansUId := range fansUIds { curPage = append(curPage, &pb.FansItem{ UserId: in.UserId, FansUserId: fansUId, - CreateTime: CreateTime[i], + CreateTime: createTime[i], }) } } else { - fanss, err = l.svcCtx.FollowModel.FindByFollowedUserId(l.ctx, in.UserId, types.CacheMaxFansCount) + fansModel, err = l.svcCtx.FollowModel.FindByFollowedUserId(l.ctx, in.UserId, types.CacheMaxFansCount) if err != nil { l.Logger.Errorf("[FansList] FollowModel.FindByFollowedUserId error: %v req: %v", err, in) return nil, err } - if len(fanss) == 0 { + if len(fansModel) == 0 { return &pb.FansListResponse{}, nil } var firstPageFans []*model.Follow - if len(fanss) > int(in.PageSize) { - firstPageFans = fanss[:in.PageSize] + if len(fansModel) > int(in.PageSize) { + firstPageFans = fansModel[:in.PageSize] } else { - firstPageFans = fanss + firstPageFans = fansModel isEnd = true } for _, fans := range firstPageFans { @@ -101,7 +101,7 @@ func (l *FansListLogic) FansList(in *pb.FansListRequest) (*pb.FansListResponse, cursor = 0 } for i, fans := range curPage { - if fans.CreateTime == in.Cursor { + if fans.CreateTime == in.Cursor && fans.FansUserId == in.Id { curPage = curPage[i:] break } @@ -131,10 +131,10 @@ func (l *FansListLogic) FansList(in *pb.FansListRequest) (*pb.FansListResponse, if !isCache { threading.GoSafe(func() { - if len(fanss) < types.CacheMaxFansCount && len(fanss) > 0 { - fanss = append(fanss, &model.Follow{UserID: -1}) + if len(fansModel) < types.CacheMaxFansCount && len(fansModel) > 0 { + fansModel = append(fansModel, &model.Follow{UserID: -1}) } - err = l.addCacheFans(context.Background(), in.UserId, fanss) + err = l.addCacheFans(context.Background(), in.UserId, fansModel) }) } return ret, nil diff --git a/application/follow/rpc/pb/follow.pb.go b/application/follow/rpc/pb/follow.pb.go index be1f54d..7140c6b 100644 --- a/application/follow/rpc/pb/follow.pb.go +++ b/application/follow/rpc/pb/follow.pb.go @@ -427,6 +427,7 @@ type FansListRequest struct { UserId int64 `protobuf:"varint,1,opt,name=userId,proto3" json:"userId,omitempty"` Cursor int64 `protobuf:"varint,2,opt,name=cursor,proto3" json:"cursor,omitempty"` PageSize int64 `protobuf:"varint,3,opt,name=pageSize,proto3" json:"pageSize,omitempty"` + Id int64 `protobuf:"varint,4,opt,name=Id,proto3" json:"Id,omitempty"` } func (x *FansListRequest) Reset() { @@ -482,6 +483,13 @@ func (x *FansListRequest) GetPageSize() int64 { return 0 } +func (x *FansListRequest) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + type FansItem struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -672,13 +680,14 @@ var file_follow_proto_rawDesc = []byte{ 0x6f, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x73, 0x45, 0x6e, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x69, 0x73, 0x45, 0x6e, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, - 0x28, 0x03, 0x52, 0x02, 0x49, 0x64, 0x22, 0x5d, 0x0a, 0x0f, 0x46, 0x61, 0x6e, 0x73, 0x4c, 0x69, + 0x28, 0x03, 0x52, 0x02, 0x49, 0x64, 0x22, 0x6d, 0x0a, 0x0f, 0x46, 0x61, 0x6e, 0x73, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x67, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x70, 0x61, 0x67, - 0x65, 0x53, 0x69, 0x7a, 0x65, 0x22, 0xa2, 0x01, 0x0a, 0x08, 0x46, 0x61, 0x6e, 0x73, 0x49, 0x74, + 0x65, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x02, 0x49, 0x64, 0x22, 0xa2, 0x01, 0x0a, 0x08, 0x46, 0x61, 0x6e, 0x73, 0x49, 0x74, 0x65, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1e, 0x0a, 0x0a, 0x66, 0x61, 0x6e, 0x73, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a,