1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:10:56 +00:00

Update redis.go

use `scan` instead of `keys`
This commit is contained in:
huija 2020-06-19 14:21:12 +08:00
parent 86935ada01
commit 40181c1042

30
cache/redis/redis.go vendored
View File

@ -139,7 +139,7 @@ func (rc *Cache) Decr(key string) error {
func (rc *Cache) ClearAll() error { func (rc *Cache) ClearAll() error {
c := rc.p.Get() c := rc.p.Get()
defer c.Close() defer c.Close()
cachedKeys, err := redis.Strings(c.Do("KEYS", rc.key+":*")) cachedKeys, err := rc.Scan(rc.key + ":*")
if err != nil { if err != nil {
return err return err
} }
@ -151,6 +151,34 @@ func (rc *Cache) ClearAll() error {
return err 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. // StartAndGC start redis cache adapter.
// config is like {"key":"collection key","conn":"connection info","dbNum":"0"} // config is like {"key":"collection key","conn":"connection info","dbNum":"0"}
// the cache item in redis are stored forever, // the cache item in redis are stored forever,