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

Method GetList

internal/repository/cache/post.go:192–214  ·  view source on GitHub ↗

GetList 获取帖子列表缓存

(ctx context.Context, page int, size int)

Source from the content-addressed store, hash-verified

190
191// GetList 获取帖子列表缓存
192func (c *postCache) GetList(ctx context.Context, page int, size int) ([]domain.Post, error) {
193 if page <= 0 || size <= 0 {
194 return nil, fmt.Errorf("无效的分页参数: page=%d, size=%d", page, size)
195 }
196
197 key := fmt.Sprintf("%d_%d", page, size)
198 data, err := c.client.Get(ctx, c.listKey(key)).Bytes()
199 if err != nil {
200 if err == redis.Nil {
201 return nil, fmt.Errorf("缓存中未找到帖子列表: %s", key)
202 }
203 return nil, fmt.Errorf("从缓存获取帖子列表失败: %v", err)
204 }
205
206 var posts []domain.Post
207 if err = json.Unmarshal(data, &posts); err != nil {
208 // 删除可能损坏的缓存数据
209 _ = c.client.Del(ctx, c.listKey(key))
210 return nil, fmt.Errorf("反序列化帖子列表数据失败: %v", err)
211 }
212
213 return posts, nil
214}
215
216// SetList 设置帖子列表缓存
217func (c *postCache) SetList(ctx context.Context, page int, size int, posts []domain.Post) error {

Callers

nothing calls this directly

Calls 3

listKeyMethod · 0.95
GetMethod · 0.65
DelMethod · 0.65

Tested by

no test coverage detected