From 0e79fc140e32aa007d5c9677a640168c2cd351e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87=E6=9D=B0?= <2707138687@qq.com> Date: Fri, 1 Sep 2023 13:02:40 +0800 Subject: [PATCH 1/2] fix: count video cache --- src/services/publish/handler.go | 39 ++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/services/publish/handler.go b/src/services/publish/handler.go index d2fd6b6..6a1d164 100644 --- a/src/services/publish/handler.go +++ b/src/services/publish/handler.go @@ -7,6 +7,7 @@ import ( "GuGoTik/src/models" "GuGoTik/src/rpc/feed" "GuGoTik/src/rpc/publish" + "GuGoTik/src/storage/cached" "GuGoTik/src/storage/database" "GuGoTik/src/storage/file" "GuGoTik/src/storage/redis" @@ -23,6 +24,7 @@ import ( "github.com/sirupsen/logrus" "math/rand" "net/http" + "strconv" "time" ) @@ -179,34 +181,59 @@ func (a PublishServiceImpl) ListVideo(ctx context.Context, req *publish.ListVide return } +// CountVideo todo:添加缓存 func (a PublishServiceImpl) CountVideo(ctx context.Context, req *publish.CountVideoRequest) (resp *publish.CountVideoResponse, err error) { ctx, span := tracing.Tracer.Start(ctx, "CountVideoService") defer span.End() logging.SetSpanWithHostname(span) logger := logging.LogService("PublishServiceImpl.CountVideo").WithContext(ctx) - var count int64 - err = database.Client.WithContext(ctx).Model(&models.Video{}).Where("user_id = ?", req.UserId).Count(&count).Error + countStringKey := fmt.Sprintf("VideoCount-%d", req.UserId) + countString, err := cached.GetWithFunc(ctx, countStringKey, + func(ctx context.Context, key string) (string, error) { + rCount, err := count(ctx, req.UserId) + return strconv.FormatInt(rCount, 10), err + }) + if err != nil { + cached.TagDelete(ctx, "VideoCount") logger.WithFields(logrus.Fields{ - "err": err, - }).Warnf("failed to count video") + "err": err, + "user_id": req.UserId, + }).Errorf("failed to count video") + logging.SetSpanError(span, err) + resp = &publish.CountVideoResponse{ StatusCode: strings.PublishServiceInnerErrorCode, StatusMsg: strings.PublishServiceInnerError, } - logging.SetSpanError(span, err) return } + rCount, _ := strconv.ParseUint(countString, 10, 64) resp = &publish.CountVideoResponse{ StatusCode: strings.ServiceOKCode, StatusMsg: strings.ServiceOK, - Count: uint32(count), + Count: uint32(rCount), } return } +func count(ctx context.Context, userId uint32) (count int64, err error) { + ctx, span := tracing.Tracer.Start(ctx, "CountVideo") + defer span.End() + logger := logging.LogService("PublishService.CountVideo").WithContext(ctx) + result := database.Client.Model(&models.Video{}).WithContext(ctx).Where("user_id = ?", userId).Count(&count) + + if result.Error != nil { + logger.WithFields(logrus.Fields{ + "err": err, + }).Errorf("Error when counting video") + logging.SetSpanError(span, err) + } + return count, result.Error +} + func (a PublishServiceImpl) CreateVideo(ctx context.Context, request *publish.CreateVideoRequest) (resp *publish.CreateVideoResponse, err error) { ctx, span := tracing.Tracer.Start(ctx, "CreateVideoService") defer span.End() From 4b54b52518b5b70e02eb85062136823217e40444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87=E6=9D=B0?= <2707138687@qq.com> Date: Fri, 1 Sep 2023 13:05:27 +0800 Subject: [PATCH 2/2] refactor: rm my todo --- src/services/publish/handler.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/publish/handler.go b/src/services/publish/handler.go index 6a1d164..77901ca 100644 --- a/src/services/publish/handler.go +++ b/src/services/publish/handler.go @@ -181,7 +181,6 @@ func (a PublishServiceImpl) ListVideo(ctx context.Context, req *publish.ListVide return } -// CountVideo todo:添加缓存 func (a PublishServiceImpl) CountVideo(ctx context.Context, req *publish.CountVideoRequest) (resp *publish.CountVideoResponse, err error) { ctx, span := tracing.Tracer.Start(ctx, "CountVideoService") defer span.End()