Delete 从本地缓存和 Redis 中删除一个或多个键
(ctx context.Context, keys ...string)
| 89 | |
| 90 | // Delete 从本地缓存和 Redis 中删除一个或多个键 |
| 91 | func (cm *CacheManager) Delete(ctx context.Context, keys ...string) error { |
| 92 | // 并发删除本地缓存和Redis |
| 93 | errChan := make(chan error, 2) |
| 94 | |
| 95 | go func() { |
| 96 | for _, key := range keys { |
| 97 | cm.localCache.Delete(key) |
| 98 | } |
| 99 | errChan <- nil |
| 100 | }() |
| 101 | |
| 102 | go func() { |
| 103 | errChan <- cm.redisClient.Del(ctx, keys...).Err() |
| 104 | }() |
| 105 | |
| 106 | // 等待两个操作完成 |
| 107 | for i := 0; i < 2; i++ { |
| 108 | if err := <-errChan; err != nil { |
| 109 | return err |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | // SetEmptyCache 缓存空对象,防止缓存穿透 |
| 117 | func (cm *CacheManager) SetEmptyCache(ctx context.Context, key string, ttl time.Duration) error { |