Skip to content

Commit

Permalink
video 索引查询优化,favor并发查询redis
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxianloveqiqi committed Aug 31, 2023
1 parent ba28a99 commit e81b153
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
26 changes: 23 additions & 3 deletions server/favor/rpc/internal/logic/favorednumofuserlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"GopherTok/server/video/rpc/types/video"
"context"
"github.com/pkg/errors"
"sync"

"GopherTok/server/favor/rpc/internal/svc"
"GopherTok/server/favor/rpc/types/favor"
Expand Down Expand Up @@ -35,13 +36,32 @@ func (l *FavoredNumOfUserLogic) FavoredNumOfUser(in *favor.FavoredNumOfUserReq)
if err != nil {
return nil, errors.Wrapf(errorx.NewDefaultError(err.Error()), "err:%v", err)
}
var sum int = 0
var wg sync.WaitGroup
var sum int64

resultChan := make(chan int64, len(list.VideoIdList))

for _, id := range list.VideoIdList {
num, _ := l.svcCtx.FavorModel.NumOfFavor(l.ctx, id)
wg.Add(1)
id := id
go func(videoID int) {
defer wg.Done()

num, _ := l.svcCtx.FavorModel.NumOfFavor(l.ctx, id)
resultChan <- int64(num)
}(int(id))
}

go func() {
wg.Wait()
close(resultChan)
}()

for num := range resultChan {
sum += num
}

return &favor.FavoredNumOfUserResp{
FavoredNumOfUser: int64(sum),
FavoredNumOfUser: sum,
}, nil
}
2 changes: 1 addition & 1 deletion server/video/rpc/internal/logic/getuservideoidlistlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewGetUserVideoIdListLogic(ctx context.Context, svcCtx *svc.ServiceContext)
func (l *GetUserVideoIdListLogic) GetUserVideoIdList(in *video.GetUserVideoIdListReq) (*video.GetUserVideoIdListResp, error) {
// todo: add your logic here and delete this line
var list []model.Video
err := l.svcCtx.MysqlDb.Where("user_id = ?", in.UserId).Find(&list).Error
err := l.svcCtx.MysqlDb.Select("id").Where("user_id = ?", in.UserId).Find(&list).Error
if err != nil {
return nil, errors.Wrapf(errorx.NewDefaultError("mysql find 错误"+err.Error()), "mysql find err:%v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/time_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestUnix(t *testing.T) {
fmt.Println(time.Now().UnixNano())
fmt.Println(time.Now().Unix())
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))

}
Expand Down

0 comments on commit e81b153

Please sign in to comment.