Skip to content

Commit

Permalink
fix: relation isfollow cache
Browse files Browse the repository at this point in the history
  • Loading branch information
liaosunny123 committed Sep 1, 2023
1 parent d633f74 commit c041c56
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/services/feed/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
amqp "github.com/rabbitmq/amqp091-go"
"gorm.io/gorm"
"strconv"
Expand Down Expand Up @@ -361,7 +362,7 @@ func (s FeedServiceImpl) QueryVideoExisted(ctx context.Context, req *feed.VideoE
logging.SetSpanWithHostname(span)
logger := logging.LogService("FeedService.QueryVideoExisted").WithContext(ctx)
var video models.Video
_, err = cached.GetWithFunc(ctx, "VideoExistedCached", func(ctx context.Context, key string) (string, error) {
_, err = cached.GetWithFunc(ctx, fmt.Sprintf("VideoExistedCached-%d", req.VideoId), func(ctx context.Context, key string) (string, error) {
row := database.Client.WithContext(ctx).Where("id = ?", req.VideoId).First(&video)
if row.Error != nil {
return "false", row.Error
Expand Down
2 changes: 2 additions & 0 deletions src/services/publish/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ func (a PublishServiceImpl) CreateVideo(ctx context.Context, request *publish.Cr
}
}

countStringKey := fmt.Sprintf("VideoCount-%d", request.ActorId)
cached.TagDelete(ctx, countStringKey)
resp = &publish.CreateVideoResponse{
StatusCode: strings.ServiceOKCode,
StatusMsg: strings.ServiceOK,
Expand Down
27 changes: 18 additions & 9 deletions src/services/relation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
grpc2 "GuGoTik/src/utils/grpc"
"GuGoTik/src/utils/logging"
"context"
"errors"
"fmt"
"github.com/go-redis/redis_rate/v10"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -206,6 +208,7 @@ func (r RelationServiceImpl) Follow(ctx context.Context, request *relation.Relat
logging.SetSpanError(span, err)
return
}
cached.TagDelete(ctx, fmt.Sprintf("IsFollowedCache-%d-%d", request.UserId, request.ActorId))
resp = &relation.RelationActionResponse{
StatusCode: strings.ServiceOKCode,
StatusMsg: strings.ServiceOK,
Expand Down Expand Up @@ -343,7 +346,7 @@ func (r RelationServiceImpl) Unfollow(ctx context.Context, request *relation.Rel
logging.SetSpanError(span, err)
return
}

cached.TagDelete(ctx, fmt.Sprintf("IsFollowedCache-%d-%d", request.UserId, request.ActorId))
resp = &relation.RelationActionResponse{
StatusCode: strings.ServiceOKCode,
StatusMsg: strings.ServiceOK,
Expand Down Expand Up @@ -740,15 +743,21 @@ func (r RelationServiceImpl) IsFollow(ctx context.Context, request *relation.IsF
logging.SetSpanWithHostname(span)
logger := logging.LogService("RelationService.isFollow").WithContext(ctx)

var count int64
result := database.Client.WithContext(ctx).
Model(&models.Relation{}).
Where("user_id = ? AND actor_id = ?", request.UserId, request.ActorId).
Count(&count)
res, err := cached.GetWithFunc(ctx, fmt.Sprintf("IsFollowedCache-%d-%d", request.UserId, request.ActorId), func(ctx context.Context, key string) (string, error) {
var count int64
row := database.Client.WithContext(ctx).
Model(&models.Relation{}).
Where("user_id = ? AND actor_id = ?", request.UserId, request.ActorId).
Count(&count)
if row.Error != nil && !errors.Is(row.Error, gorm.ErrRecordNotFound) {
return "false", row.Error
}
return strconv.FormatInt(count, 10), nil
})

if result.Error != nil {
if err != nil {
logger.WithFields(logrus.Fields{
"err": result.Error,
"err": err,
"ActorId": request.ActorId,
"UserId": request.UserId,
}).Errorf("IsFollowService failed")
Expand All @@ -765,7 +774,7 @@ func (r RelationServiceImpl) IsFollow(ctx context.Context, request *relation.IsF
resp = &relation.IsFollowResponse{
StatusCode: strings.ServiceOKCode,
StatusMsg: strings.ServiceOK,
Result: count > 0,
Result: res != "0",
}
return
}
Expand Down

0 comments on commit c041c56

Please sign in to comment.