Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix count video cache #219

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/services/publish/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/sirupsen/logrus"
"math/rand"
"net/http"
"strconv"
"time"
)

Expand Down Expand Up @@ -185,28 +187,52 @@ func (a PublishServiceImpl) CountVideo(ctx context.Context, req *publish.CountVi
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()
Expand Down
Loading