{"id":192,"date":"2021-06-30T18:18:19","date_gmt":"2021-06-30T10:18:19","guid":{"rendered":"https:\/\/www.92k.xin\/?p=192"},"modified":"2021-06-30T18:21:07","modified_gmt":"2021-06-30T10:21:07","slug":"golang-%e5%85%a8%e9%81%8d%e5%8e%86redis","status":"publish","type":"post","link":"https:\/\/www.92k.xin\/?p=192","title":{"rendered":"Golang \u5168\u904d\u5386Redis"},"content":{"rendered":"\n

\u5148\u4e0a\u4ee3\u7801\uff0c\u7528\u7684\u662f <\/p>\n\n\n\n

github.com\/go-redis\/redis<\/pre>\n\n\n\n

\u5148\u770b\u4ee3\u7801<\/p>\n\n\n\n

package main\n\nimport (\n\t\"fmt\"\n\t\"sync\"\n\t\"time\"\n\n\t\"github.com\/go-redis\/redis\"\n)\n\n\nfunc main() {\n\tclient := redis.NewClient(&redis.Options{\n\t\tAddr:     \"127.0.0.1:6379\",\n\t\tPassword: \"\",\n\t\tDB:       0,\n\t})\n\tdefer client.Close()\n        \/\/\u539f\u59cb\u4f4d\u7f6e\n\tIndex := uint64(0)\n\tvar RedisKeys []string\n\tvar ScanError error\n\tTimeBegin := time.Now().UnixNano()\n\tvar wg sync.WaitGroup\n\tfor {\n\t\tRedisKeys, Index, ScanError = client.Scan(Index, \"*\", 500).Result()\n\t\tif ScanError != nil {\n\t\t\tfmt.Println(ScanError.Error())\n\t\t\treturn\n\t\t}\n\t\twg.Add(1)\n\t\tgo func(keys []string) {\n\t\t\tfor _, key := range keys {\n\t\t\t\tTypeName, TypeError := client.Type(key).Result()\n\t\t\t\tif TypeError != nil {\n\t\t\t\t\tfmt.Println(TypeError.Error())\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tif TypeName == \"string\" {\n\t\t\t\t\tfmt.Println(\"[\", TypeName, \"]\", key, client.Get(key).Val())\n\t\t\t\t} else if TypeName == \"hash\" { \/\/\u54c8\u5e0c\n\t\t\t\t\tfmt.Println(\"[\", TypeName, \"]\", key, client.HGetAll(key).Val())\n\t\t\t\t} else if TypeName == \"list\" { \/\/\u5217\u8868\n\t\t\t\t\tfmt.Println(\"[\", TypeName, \"]\", key, client.LRange(key, 0, -1).Val())\n\t\t\t\t} else if TypeName == \"set\" {\n\t\t\t\t\tfmt.Println(\"[\", TypeName, \"]\", key, client.SCard(key).Val())\n\t\t\t\t} else if TypeName == \"none\" {\n\t\t\t\t\tfmt.Println(\"[\", TypeName, \"]\", key, \"\u4e0d\u5b58\u5728\")\n\t\t\t\t} else {\n\t\t\t\t\tfmt.Println(\"[\", TypeName, \"]\", key, client.Type(key).String())\n\t\t\t\t}\n\t\t\t}\n\t\t\twg.Done()\n\t\t}(RedisKeys)\n\t\tif Index == 0 {\n\t\t\tbreak\n\t\t}\n\t}\n\twg.Wait()\n        \/\/\u8017\u65f6\u8ba1\u7b97\n\tfmt.Println(float64(time.Now().UnixNano()-TimeBegin) \/ float64(1000000000))\n}\n<\/pre><\/div>\n\n\n\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5148\u4e0a\u4ee3\u7801\uff0c\u7528\u7684\u662f github.com\/go-redis\/redis \u5148\u770b\u4ee3\u7801 package main im… <\/p>\n