Go 通过链接池来操作 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()
_,err
:= conn
.Do("set","name","tom")
if err
!=nil{
fmt
.Println("conn err=",err
)
return
}
result
,err
:= conn
.Do("get","name")
if err
!=nil{
fmt
.Println("conn.Do get=",err
)
return
}
fmt
.Println("get 到的值=",result
)
}
转载请注明原文地址:https://tech.qufami.com/read-18290.html