MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / Get

Method Get

internal/repository/cache/post.go:58–83  ·  view source on GitHub ↗

Get 获取帖子缓存

(ctx context.Context, key string)

Source from the content-addressed store, hash-verified

56
57// Get 获取帖子缓存
58func (c *postCache) Get(ctx context.Context, key string) (domain.Post, error) {
59 if key == "" {
60 return domain.Post{}, fmt.Errorf("无效的帖子ID: %s", key)
61 }
62
63 // 先检查是否是空对象
64 isEmpty, err := c.IsEmpty(ctx, key)
65 if err == nil && isEmpty {
66 return domain.Post{}, fmt.Errorf("帖子不存在 %s", key)
67 }
68
69 data, err := c.client.Get(ctx, c.key(key)).Bytes()
70 if err != nil {
71 if err == redis.Nil {
72 return domain.Post{}, fmt.Errorf("缓存中未找到帖子 %s", key)
73 }
74 return domain.Post{}, fmt.Errorf("从缓存获取帖子失败: %v", err)
75 }
76
77 var post domain.Post
78 if err = json.Unmarshal(data, &post); err != nil {
79 return domain.Post{}, fmt.Errorf("反序列化帖子数据失败: %v", err)
80 }
81
82 return post, nil
83}
84
85// Set 设置帖子缓存,使用随机过期时间防止缓存雪崩
86func (c *postCache) Set(ctx context.Context, key string, post domain.Post) error {

Callers

nothing calls this directly

Calls 3

IsEmptyMethod · 0.95
keyMethod · 0.95
GetMethod · 0.65

Tested by

no test coverage detected