DeleteCacheByPrefix removes all cache entries whose keys start with the given prefix. Uses SCAN to avoid blocking Redis on large datasets.
(prefix string)
| 36 | // DeleteCacheByPrefix removes all cache entries whose keys start with the given prefix. |
| 37 | // Uses SCAN to avoid blocking Redis on large datasets. |
| 38 | func (p *provider) DeleteCacheByPrefix(prefix string) error { |
| 39 | pattern := cachePrefix + prefix + "*" |
| 40 | var cursor uint64 |
| 41 | for { |
| 42 | keys, nextCursor, err := p.store.Scan(p.ctx, cursor, pattern, 100).Result() |
| 43 | if err != nil { |
| 44 | p.dependencies.Log.Debug().Err(err).Msg("Error scanning cache keys from redis") |
| 45 | return err |
| 46 | } |
| 47 | if len(keys) > 0 { |
| 48 | if err := p.store.Del(p.ctx, keys...).Err(); err != nil { |
| 49 | p.dependencies.Log.Debug().Err(err).Msg("Error deleting cache keys from redis") |
| 50 | return err |
| 51 | } |
| 52 | } |
| 53 | cursor = nextCursor |
| 54 | if cursor == 0 { |
| 55 | break |
| 56 | } |
| 57 | } |
| 58 | return nil |
| 59 | } |