Get 从redis中获取数据并反序列化
(ctx context.Context, postId int64)
| 29 | |
| 30 | // Get 从redis中获取数据并反序列化 |
| 31 | func (u *commentCache) Get(ctx context.Context, postId int64) (domain.Comment, error) { |
| 32 | var dc domain.Comment |
| 33 | key := fmt.Sprintf("linkme:comment:%d", postId) |
| 34 | // 从redis中读取数据 |
| 35 | data, err := u.cmd.Get(ctx, key).Result() |
| 36 | if err != nil { |
| 37 | if err == redis.Nil { |
| 38 | return domain.Comment{}, err |
| 39 | } |
| 40 | return domain.Comment{}, err |
| 41 | } |
| 42 | if err = json.Unmarshal([]byte(data), &dc); err != nil { |
| 43 | return domain.Comment{}, fmt.Errorf("反序列化评论数据失败: %v", err) |
| 44 | } |
| 45 | return dc, nil |
| 46 | } |
| 47 | |
| 48 | // Set 将传入的du结构体序列化存入redis中 |
| 49 | func (u *commentCache) Set(ctx context.Context, dc domain.Comment) error { |