golang使用redis与连接池
使用下载go的redis包go get github.com/gomodule/redigo/redis 如果网不好的话就很费劲了
package main import ( "fmt" "github.com/gomodule/redigo/redis" // 引入redis包 ) func main() { //连接数据源 rediss, err := redis.dial("tcp", "127.0.01:6379") if err != nil { fmt.println("连接异常", err) } //插入string数据 test, err := rediss.do("set", "test", "hi") if err != nil { fmt.println("插入数据失败", err) } fmt.println(test) //读取string数据 str, err := redis.string(rediss.do("get", "test")) fmt.println(str) //hash 类型 do, _ := rediss.do("hset", "hh", "name", "zhangsn") fmt.println(do) hh, _ := redis.string(rediss.do("hget", "hh", "name")) fmt.println(hh) //设置key 过期时间 rediss.do("expire", "hh", 1) //关闭redis rediss.close() }
redis数据源连接池
package main import ( "fmt" "github.com/gomodule/redigo/redis" // 引入redis包 ) var pool *redis.pool func init() { pool = &redis.pool{ maxidle: 8, //最大空闲连接数 maxactive: 0, //表示和数据库最大连接数。0表示没有限制 idletimeout: 100, //最大空闲时间 dial: func() (redis.conn, error) { //初始化连接 redis 地址 return redis.dial("tcp", "127.0.01:6379") }, } } func main() { //获取连接 conn := pool.get() //插入数据 do, err := conn.do("set", "11", "11") if err != nil { fmt.println("插入失败", err) } fmt.println(do) //关闭redis conn.close() }
golang redis连接池封装
创建连接池方法文件
package dao import ( "fmt" "github.com/gomodule/redigo/redis" "gopkg.in/ini.v1" "os" "sync" "time" ) var once sync.once // redisclient redis 服务 type redisclient struct { client *redis.pool } //redis 全局 redis var redispool *redisclient //connectredis 连接 redis 数据库,设置全局的 redis 对象 func connectredis() { config, err := ini.load("./config/app.ini") if err != nil { //失败 fmt.printf("fail to read file: %v", err) os.exit(1) } address := config.section("redis").key("address").string() password := config.section("redis").key("password").string() db, _ := config.section("redis").key("db").int() once.do(func() { redispool = newclient(address, password, db) }) con_err := redispool.ping() if con_err != nil { panic(con_err) } } // newclient 创建一个新的 redis 连接 func newclient(address string, password string, db int) *redisclient { // 初始化自定的 redisclient 实例 rds := &redisclient{} // 使用 redis 库里的 newclient 初始化连接 rds.client = &redis.pool{ maxidle: 100, //最大空闲 maxactive: 1000, //最大连接 idletimeout: time.duration(60) * time.second, wait: true, dial: func() (redis.conn, error) { c, err := redis.dial( "tcp", address, redis.dialpassword(password), redis.dialdatabase(int(db)), redis.dialconnecttimeout(time.duration(60)*time.second), redis.dialreadtimeout(time.duration(60)*time.second), redis.dialwritetimeout(time.duration(60)*time.second), ) if err != nil { return nil, err } return c, err }, } return rds } // ping 用以测试 redis 连接是否正常 func (rds *redisclient) ping() error { _, err := rds.client.get().do("ping") return err } // set 存储 key 对应的 value,且设置 expiration 过期时间(单位纳秒) func (rds *redisclient) setex(key string, expiration int, value interface{}) bool { conn := rds.client.get() defer conn.close() if _, err := conn.do("setex", key, expiration, value); err != nil { fmt.println(err) return false } return true } // //get 获取 key 对应的 value func (rds *redisclient) get(key string) string { conn := rds.client.get() defer conn.close() result, err := redis.string(conn.do("get", key)) if err != nil { return "" } return result } //get 获取 key 对应的 value func (rds *redisclient) rpop(key string) (string, error) { conn := rds.client.get() defer conn.close() result, err := redis.string(conn.do("rpop", key)) if err != nil { return "", err } return result, nil }
配置文件
app_name = go-gin [mysql] ip = 127.0.0.1 port = 3306 user = root password = root database = test prefix = tt_ #最大连接数 maxidleconns = 500 #最大空闲 maxopenconns = 50 [redis] address = 127.0.0.1:6379 password = 123456 db = 7
调用方法
func main() { dao.connectredis() //初始化连接redis defer dao.redispool.client.close() //退出前执行关闭 res, err := dao.redispool.rpop("aaa") if err != nil { fmt.println(err) } fmt.println(res) }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论