Skip to content

Commit

Permalink
feat: comment all the Redis TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
zero1777 authored and alice890308 committed Mar 5, 2024
1 parent 4698cac commit 68e627f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
4 changes: 2 additions & 2 deletions modules/comment/dao/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dao
import (
"context"
"errors"
"fmt"
"time"

"github.com/NTHU-LSALAB/NTHU-Distributed-System/modules/comment/pb"
Expand Down Expand Up @@ -42,8 +41,9 @@ var (
ErrCommentNotFound = errors.New("comment not found")
)

// key format: "listComment:{videoID}:{limit}:{offset}"
func listCommentKey(videoID string, limit, offset int) string {
return fmt.Sprintf("listComment:%s:%d:%d", videoID, limit, offset)
// Redis TODO
}

func NewFakeComment(videoID string) *Comment {
Expand Down
41 changes: 15 additions & 26 deletions modules/comment/dao/comment_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,35 @@ const (
commentDAORedisCacheDuration = 3 * time.Minute
)

// create redisCommentDAO by cache and baseDAO
func NewRedisCommentDAO(client *rediskit.RedisClient, baseDAO CommentDAO) *redisCommentDAO {
return &redisCommentDAO{
cache: cache.New(&cache.Options{
Redis: client,
LocalCache: cache.NewTinyLFU(commentDAOLocalCacheSize, commentDAOLocalCacheDuration),
}),
baseDAO: baseDAO,
}
// Redis TODO
}

// List all the comments related to the videoID
// Notice that all the comments will be stored as a single value in the cache
// The key is generated by listCommentKey function in comment.go
// The implementation should handle both cache miss and cache hit scenarios
func (dao *redisCommentDAO) ListByVideoID(ctx context.Context, videoID string, limit, offset int) ([]*Comment, error) {
var comment []*Comment

if err := dao.cache.Once(&cache.Item{
Key: listCommentKey(videoID, limit, offset),
Value: &comment,
TTL: commentDAORedisCacheDuration,
Do: func(*cache.Item) (interface{}, error) {
return dao.baseDAO.ListByVideoID(ctx, videoID, limit, offset)
},
}); err != nil {
return nil, err
}
return comment, nil
// Redis TODO
}

// The following operations are not cachable, just pass down to baseDAO

// The operation are not cacheable, just pass down to baseDAO
func (dao *redisCommentDAO) Create(ctx context.Context, comment *Comment) (uuid.UUID, error) {
return dao.baseDAO.Create(ctx, comment)
// Redis TODO
}

// The following operations are not cacheable, just pass down to baseDAO
func (dao *redisCommentDAO) Update(ctx context.Context, comment *Comment) error {
return dao.baseDAO.Update(ctx, comment)
// Redis TODO
}

// The following operations are not cacheable, just pass down to baseDAO
func (dao *redisCommentDAO) Delete(ctx context.Context, id uuid.UUID) error {
return dao.baseDAO.Delete(ctx, id)
// Redis TODO
}

// The following operations are not cacheable, just pass down to baseDAO
func (dao *redisCommentDAO) DeleteByVideoID(ctx context.Context, videoID string) error {
return dao.baseDAO.DeleteByVideoID(ctx, videoID)
// Redis TODO
}

0 comments on commit 68e627f

Please sign in to comment.