前言
Go语言通过github.com/garyburd/redigo/redis
包操作Redis学习笔记
下载依赖
1
| go get github.com/garyburd/redigo/redis
|
获取Redis连接
1
| conn, err := redis.Dial("tcp", "127.0.0.1:6379")
|
延迟关闭Redis连接
执行Redis操作
增删改
1
| _, err := conn.Do("set", "key", "value")
|
查
断言为int类型
1
| result, res := redis.Int(conn.Do("get", "key"))
|
断言为float类型
1
| result, ree := redis.Float(conn.Do("get", "key"))
|
断言为string类型
1
| result, ree := redis.String(conn.Do("get", "key"))
|
返回多个字符串
1
| result, ree := redis.Strings(conn.Do("get", "key", "key"))
|
连接池
定义连接池
MaxIdle
:设置最大空闲连接数
MaxActive
:设置和数据库的最大连接数,如果为0表示无限制
IdleTimeout
:最大空闲时间
1 2 3 4 5 6 7 8 9
| var pool *redis.Pool pool = &redis.Pool{ MaxIdle: 8, MaxActive: 0, IdleTimeout: 100, Dial: func() (redis.Conn, err) { return redis.Dial("tcp", "127.0.0.1:6379") }, }
|
从连接池取出一个连接
延迟关闭连接
关闭连接池
完成
参考文献
哔哩哔哩——尚硅谷