diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 372dd48b..e70c745b 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -139,7 +139,7 @@ func (rc *Cache) Decr(key string) error { func (rc *Cache) ClearAll() error { c := rc.p.Get() defer c.Close() - cachedKeys, err := redis.Strings(c.Do("KEYS", rc.key+":*")) + cachedKeys, err := rc.Scan(rc.key + ":*") if err != nil { return err } @@ -151,6 +151,34 @@ func (rc *Cache) ClearAll() error { return err } +func (rc *Cache) Scan(pattern string) (keys []string, err error) { + c := rc.p.Get() + defer c.Close() + var ( + cursor uint64 = 0 // start + result []interface{} + list []string + ) + for { + result, err = redis.Values(c.Do("SCAN", cursor, "MATCH", pattern, "COUNT", 1024)) + if err != nil { + return + } + list, err = redis.Strings(result[1], nil) + if err != nil { + return + } + keys = append(keys, list...) + cursor, err = redis.Uint64(result[0], nil) + if err != nil { + return + } + if cursor == 0 { // over + return + } + } +} + // StartAndGC start redis cache adapter. // config is like {"key":"collection key","conn":"connection info","dbNum":"0"} // the cache item in redis are stored forever,