背景
最近为了做实验,需要redis中有百万的数据。于是乎,想到用redis的go client。
直接使用string
package main
import ( "fmt" "github.com/go-redis/redis" "strconv" "time" ) var redisClient *redis.Client func init() { var err error redisClient=redis.NewClient(&redis.Options{ Addr:"192.168.172.14:6379", Password:"", DB:0, PoolSize: 100, //这个参数必须配置,否则会报超时的错 WriteTimeout: 300*time.Second, ReadTimeout: 300*time.Second, }) pong,err:=redisClient.Ping().Result() if err!=nil { panic(err) } fmt.Println("connect result:",pong,err) }
func main(){ fmt.Println(time.Now().Format("2006-01-02 15:04:00.000")) pipe:=redisClient.Pipeline() for i:=0;i<1000000;i++ { pipe.Set("test_"+strconv.Itoa(i),"value_"+strconv.Itoa(i),time.Hour) } _, err := pipe.Exec() if err!=nil { fmt.Println(err) } fmt.Println(time.Now().Format("2006-01-02 15:04:00.000")) fmt.Println(redisClient.Dbsize().Val()) }
|
用set集合
import ( "fmt" "github.com/go-redis/redis" "strconv" "time" ) var redisClient *redis.Client func init() { var err error redisClient=redis.NewClient(&redis.Options{ Addr:"127.0.0.1:6379", Password:"", DB:0, PoolSize: 100, WriteTimeout: 300*time.Second, ReadTimeout: 300*time.Second, }) pong,err:=redisClient.Ping().Result() if err!=nil { panic(err) } fmt.Println("connect result:",pong,err) }
func main(){ fmt.Println(time.Now().Format("2006-01-02 15:04:00.000")) pipe:=redisClient.Pipeline() for i:=0;i<2000000;i++ { pipe.SAdd("test","value_"+strconv.Itoa(i)) } _, err := pipe.Exec() if err!=nil { fmt.Println(err) } fmt.Println(time.Now().Format("2006-01-02 15:04:00.000")) fmt.Println(redisClient.SCard("test").Val()) }
|