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

Method DelPubList

internal/repository/cache/post.go:326–372  ·  view source on GitHub ↗

DelPubList 删除已发布帖子列表缓存

(ctx context.Context, key string)

Source from the content-addressed store, hash-verified

324
325// DelPubList 删除已发布帖子列表缓存
326func (c *postCache) DelPubList(ctx context.Context, key string) error {
327 if key == "*" {
328 pattern := c.listPubPrefix + key
329 var cursor uint64
330 var keys []string
331
332 for {
333 var scanKeys []string
334 var err error
335
336 // 每次扫描100个key
337 scanKeys, cursor, err = c.client.Scan(ctx, cursor, pattern, 100).Result()
338 if err != nil {
339 return fmt.Errorf("扫描缓存键失败: %v", err)
340 }
341
342 keys = append(keys, scanKeys...)
343
344 // 如果游标为0,表示扫描完成
345 if cursor == 0 {
346 break
347 }
348 }
349
350 // 如果有key需要删除,则使用pipeline批量删除
351 if len(keys) > 0 {
352 var err error
353 for i := 0; i < 3; i++ { // 重试3次
354 pipe := c.client.Pipeline()
355 for _, key := range keys {
356 pipe.Del(ctx, key)
357 }
358
359 if _, err = pipe.Exec(ctx); err == nil {
360 break
361 }
362
363 if i == 2 { // 最后一次重试失败
364 return fmt.Errorf("批量删除缓存键失败(重试3次): %v", err)
365 }
366 time.Sleep(time.Millisecond * 50) // 失败后短暂等待再重试
367 }
368 }
369 return nil
370 }
371 return c.client.Del(ctx, c.pubListKey(key)).Err()
372}
373
374// GetPub 获取已发布帖子缓存
375func (c *postCache) GetPub(ctx context.Context, key string) (domain.Post, error) {

Callers

nothing calls this directly

Calls 3

pubListKeyMethod · 0.95
DelMethod · 0.65
ExecMethod · 0.65

Tested by

no test coverage detected