Get All keys From redis in go
Use the Strings function to convert the result to a slice of strings:
keys, err := redis.Strings(cn.Do("KEYS", "*"))
if err != nil {
// handle error
}
for _, key := range keys {
fmt.Println(key)
}
Since Redis only has one thread, the KEYS
command will block all other requests until it has finished, so it's not a good approach for production. Instead, use SCAN
. see SCAN documentation here