Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

style: rename comment key #117

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
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
23 changes: 14 additions & 9 deletions cmd/interaction/dal/cache/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (

func GetComments(ctx context.Context, key string) (comments *[]redis.Z, err error) {
pipe := RedisClient.TxPipeline()
err = pipe.TTL(ctx, key).Err()
commentKey := GetCommentKey(key)
err = pipe.TTL(ctx, commentKey).Err()
if err != nil {
klog.Error(err)
return nil, err
}
err = pipe.ZRevRangeWithScores(ctx, key, 0, -1).Err()
err = pipe.ZRevRangeWithScores(ctx, commentKey, 0, -1).Err()
if err != nil {
klog.Error(err)
return nil, err
Expand All @@ -36,7 +37,7 @@ func GetComments(ctx context.Context, key string) (comments *[]redis.Z, err erro
lastTime := cmders[0].(*redis.DurationCmd).Val()
rComments := cmders[1].(*redis.ZSliceCmd).Val()
if lastTime < constants.CommentExpiredTime/2 {
err = RedisClient.Expire(ctx, key, constants.CommentExpiredTime).Err()
err = RedisClient.Expire(ctx, commentKey, constants.CommentExpiredTime).Err()
if err != nil {
klog.Error(err)
return nil, err
Expand All @@ -62,6 +63,7 @@ func AddComment(ctx context.Context, key string, comment *db.Comment) (err error
}

func AddComments(ctx context.Context, key string, comments *[]db.Comment) (err error) {
commentKey := GetCommentKey(key)
zComments := make([]redis.Z, len(*comments))
for i := 0; i < len(*comments); i++ {
data, err := (*comments)[i].MarshalMsg(nil)
Expand All @@ -72,12 +74,12 @@ func AddComments(ctx context.Context, key string, comments *[]db.Comment) (err e
zComments[i] = redis.Z{Score: float64((*comments)[i].CreatedAt.Unix()), Member: data}
}
pipe := RedisClient.TxPipeline()
err = pipe.ZAdd(ctx, key, zComments...).Err()
err = pipe.ZAdd(ctx, commentKey, zComments...).Err()
if err != nil {
klog.Error(err)
return err
}
err = pipe.Expire(ctx, key, constants.CommentExpiredTime).Err()
err = pipe.Expire(ctx, commentKey, constants.CommentExpiredTime).Err()
if err != nil {
klog.Error(err)
return err
Expand All @@ -101,12 +103,13 @@ func AddComments(ctx context.Context, key string, comments *[]db.Comment) (err e
func AddNoData(ctx context.Context, key string) (err error) {
zData := redis.Z{}
pipe := RedisClient.TxPipeline()
err = pipe.ZAdd(ctx, key, zData).Err()
commentKey := GetCommentKey(key)
err = pipe.ZAdd(ctx, commentKey, zData).Err()
if err != nil {
klog.Error(err)
return err
}
err = pipe.Expire(ctx, key, constants.NoDataExpiredTime).Err()
err = pipe.Expire(ctx, commentKey, constants.NoDataExpiredTime).Err()
if err != nil {
klog.Error(err)
return err
Expand Down Expand Up @@ -167,7 +170,8 @@ func SetCount(ctx context.Context, key string, count int64) (err error) {
}

func IsExistComment(ctx context.Context, key string) (exist int64, err error) {
exist, err = RedisClient.Exists(ctx, key).Result()
commentKey := GetCommentKey(key)
exist, err = RedisClient.Exists(ctx, commentKey).Result()
if err != nil {
klog.Error(err)
} else {
Expand All @@ -187,7 +191,8 @@ func Delete(ctx context.Context, key string) (err error) {
}

func Unlink(ctx context.Context, key string) (err error) {
err = RedisClient.Unlink(ctx, key).Err()
commentKey := GetCommentKey(key)
err = RedisClient.Unlink(ctx, commentKey).Err()
if err != nil {
klog.Error(err)
} else {
Expand Down
6 changes: 5 additions & 1 deletion cmd/interaction/dal/cache/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func GetCommentNXKey(videoID string) string {
}

func GetCountKey(videoID string) string {
return fmt.Sprintf("%s:%s", constants.CommentCountKey, videoID)
return fmt.Sprintf("%s:%s", constants.CountKey, videoID)
}

func GetCommentKey(videoID string) string {
return fmt.Sprintf("%s:%s", constants.CommentKey, videoID)
}

func GetCountNXKey(videoID string) string {
Expand Down
3 changes: 2 additions & 1 deletion pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const (
LockTime = 1 * time.Second
LockWaitTime = 5 * time.Millisecond
MaxRetryTimes = 3
CommentCountKey = "count"
CountKey = "count"
CommentKey = "comment"
CommentNXKey = "commentNX"
CountNXKey = "countNX"
// RPC
Expand Down
Loading