连接池操作
import (
"fmt"
redis "redisgo/redis"
)
func redisConnPool() *redis.Pool {
setdb := redis.DialDatabase(0) // db num
var pool *redis.Pool
pool = &redis.Pool{
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp","127.0.0.1:6379",setdb) //初始化连接
},
MaxIdle:4, // 最大空闲连接数
MaxActive:0, // 最大连接数,0是没有限制
IdleTimeout:100, // 最大空闲时间
}
return pool
}
func redisCmd(r redis.Conn) {
if ret,err := r.Do("Get","sxj") ; err != nil { // Get
fmt.Println(err)
} else {
if res,ok := ret.([]uint8) ; ok {
fmt.Println(string(res))
} else {
fmt.Println("nothing")
}
}
if _,err := r.Do("Set","test_go","haha") ; err == nil { // Set
fmt.Println("ok")
}
}
func main() {
rPool := redisConnPool()
defer rPool.Close()
r := rPool.Get()
redisCmd(r)
}