redis 连接池_golang中redis连接池的问题

4f71feee749b9131707a85c53eb83fb6.png

redis连接池的疑问,代码如下

package utilsimport (    red "github.com/gomodule/redigo/redis"    "time"    "fmt")type Redis struct {    pool     *red.Pool}var redis *Redisfunc initRedis() {    redis = new(Redis)    redis.pool = &red.Pool{        MaxIdle:     256,        MaxActive:   0,        IdleTimeout: time.Duration(120),        Dial: func() (red.Conn, error) {            return red.Dial(                "tcp",                "127.0.0.1:6379",                red.DialReadTimeout(time.Duration(1000)*time.Millisecond),                red.DialWriteTimeout(time.Duration(1000)*time.Millisecond),                red.DialConnectTimeout(time.Duration(1000)*time.Millisecond),                red.DialDatabase(0),                //red.DialPassword(""),            )        },    }}func Exec(cmd string, key interface{}, args ...interface{}) (interface{}, error) {    con := redis.pool.Get()    if err := con.Err(); err != nil {        return nil, err    }    defer con.Close()    parmas := make([]interface{}, 0)    parmas = append(parmas, key)    if len(args) > 0 {        for _, v := range args {            parmas = append(parmas, v)        }    }    return con.Do(cmd, parmas...)}func main() {    initRedis()    Exec("set","hello","world")    fmt.Print(2)    result,err := Exec("get","hello")    if err != nil {        fmt.Print(err.Error())    }    str,_:=red.String(result,err)    fmt.Print(str)}

这样操作后,每次调用initRedis()是不是相当于重新执行了一次redis连接?


1000道程序员常见问题解析是最近新整理的一个专栏,欢迎大家提供问题与思路。

所有问题、思路、答案将整理在公众号,大家可以点击:http://weixin.qq.com/r/JjokPIrEpdfarc5f9280关注,相互交流学习。

关注即可免费领取一份2020技术学习提升大礼包!(含文档、电子书、视频等等)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值