泡面师

  • 首页
  • 记录
好记性不如烂笔头
  1. 首页
  2. 记录
  3. golang
  4. 正文

golang使用redis连接池

2021-05-25 2872点热度 0人点赞 0条评论

安装

go get github.com/gomodule/redigo/redis

初始化

import (
    "github.com/gomodule/redigo/redis"
)

var pool *redis.Pool
func init() {
    pool = &redis.Pool{ //实例化一个连接池
        MaxIdle: 16, //初始连接数
        MaxActive:   0,   //池的最大连接数,0为自动
        IdleTimeout: 300, //连接超时关闭时间
        Dial: func() (redis.Conn, error) { //redis数据库信息
            return redis.Dial(
                "tcp",
                "127.0.0.1:6379",
                redis.DialReadTimeout(time.Duration(1000)*time.Millisecond),
                redis.DialWriteTimeout(time.Duration(1000)*time.Millisecond),
                redis.DialConnectTimeout(time.Duration(1000)*time.Millisecond),
                redis.DialDatabase(6),//选择redis的某库
                redis.DialPassword("123456"))//redis连接密码,如果有
        },
    }
}

使用

func main(){
    r := pool.Get()
    defer r.Close()
    r.Do("SET", "test", "abc")
    testString, _ := redis.String(r.Do("GET", "test"))
    r.Do("SET", "test", 1)
    testInt, _ := redis.Int64(r.Do("GET", "test"))


}
标签: go-redis redigo redis
最后更新:2021-05-25

泡面师

这个人很懒,什么都没留下

点赞
下一篇 >

文章评论

取消回复

COPYRIGHT © 2024 泡面师. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS