golang--redis连接池
通过golang对redis操作,还可以通过redis连接池,流程如下:
(1)事先初始化一定数量的连接,投入到连接池;
(2)当go需要操作redis时,直接从连接池取出连接即可;
(3)这样可以节省临时获取redis的时间,从而提高效率;
package main import ( "fmt" "github.com/garyburd/redigo/redis" ) var pool *redis.Pool func init() { pool = &redis.Pool{ MaxIdle: 8, MaxActive: 0, IdleTimeout: 100, Dial: func() (redis.Conn, error) { return redis.Dial("tcp", "localhost:6379") }, } } func main() { conn := pool.Get() defer conn.Close() _, err1 := conn.Do("HMSet", "user1", "name", "beijing", "address", "beijing") if err1 != nil { fmt.Println("HMSet err=", err1) return } _, err3 := conn.Do("HMSet", "user2", "name", "wuhan", "address", "wuhan") if err3 != nil { fmt.Println("HMSet err=", err3) return } //向redis读取数据,返回的r是个空接口 r, err2 := redis.Strings(conn.Do("HMGet", "user1", "name", "address")) if err2 != nil { fmt.Println("HMGet err=", err2) return } for i, v := range r { fmt.Printf("r[%d]=%v\n", i, v) } }