package main import ( "fmt" "time" redigo "github.com/gomodule/redigo/redis" "myschools.me/suguo/tutorial-redis/conf" ) func main() { pool := &redigo.Pool{ Dial: func() (redigo.Conn, error) { return redigo.Dial("tcp", conf.Host, redigo.DialDatabase(conf.Database), redigo.DialPassword(conf.Password), ) }, TestOnBorrow: func(c redigo.Conn, t time.Time) error { if time.Since(t) < time.Minute { return nil } _, err := c.Do("PING") return err }, MaxIdle: 100, MaxActive: 100, IdleTimeout: 60 * time.Second, Wait: false, MaxConnLifetime: 0, } fmt.Printf("连接数:%d\n", pool.ActiveCount()) conn := pool.Get() defer conn.Close() //判断key是否存在 a, _ := conn.Do("EXISTS", "cp01") i := a.(int64) fmt.Printf("cp01的key是否存在:%d\n", i) if i > 0 { data, err := redigo.Bytes(conn.Do("GET", "cp01")) if err != nil { fmt.Printf("GET redis错误: %s\n", err.Error()) } fmt.Printf("cp01内容:%s\n", data) } //设置一个值,单位s r, err := conn.Do("SETEX", "cp01", 600, []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")) if err != nil { fmt.Printf("写入redis错误: %s\n", err.Error()) } fmt.Printf("cp01写入后的返回:%v\n", r) }