先上代碼,用的是
1 |
github.com/go-redis/redis |
先看代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package main import ( "fmt" "sync" "time" "github.com/go-redis/redis" ) func main() { client := redis.NewClient(&redis.Options{ Addr: "127.0.0.1:6379", Password: "", DB: 0, }) defer client.Close() //原始位置 Index := uint64(0) var RedisKeys []string var ScanError error TimeBegin := time.Now().UnixNano() var wg sync.WaitGroup for { RedisKeys, Index, ScanError = client.Scan(Index, "*", 500).Result() if ScanError != nil { fmt.Println(ScanError.Error()) return } wg.Add(1) go func(keys []string) { for _, key := range keys { TypeName, TypeError := client.Type(key).Result() if TypeError != nil { fmt.Println(TypeError.Error()) break } if TypeName == "string" { fmt.Println("[", TypeName, "]", key, client.Get(key).Val()) } else if TypeName == "hash" { //哈希 fmt.Println("[", TypeName, "]", key, client.HGetAll(key).Val()) } else if TypeName == "list" { //列表 fmt.Println("[", TypeName, "]", key, client.LRange(key, 0, -1).Val()) } else if TypeName == "set" { fmt.Println("[", TypeName, "]", key, client.SCard(key).Val()) } else if TypeName == "none" { fmt.Println("[", TypeName, "]", key, "不存在") } else { fmt.Println("[", TypeName, "]", key, client.Type(key).String()) } } wg.Done() }(RedisKeys) if Index == 0 { break } } wg.Wait() //耗時(shí)計(jì)算 fmt.Println(float64(time.Now().UnixNano()-TimeBegin) / float64(1000000000)) } |