redigo长连接/复用
来源:互联网 发布:数据库基础教程 清华 编辑:程序博客网 时间:2024/05/17 00:50
Connecting to Redigo and manipulating data inside a function is easy like butter, but the problem comes when you have to re-use its connection, obviously for performance/practicality reasons.
How do you go about re-using (not recreating everytime) a Redigo connection?
最佳答案:
The best way turned out to be using Pools, which are briefly documented here: Redigo Pools.
A global variable won't eventually reuse a connection, so I ended up with something like this (using Pools as noted before):
func newPool() *redis.Pool {return &redis.Pool{ MaxIdle: 80, MaxActive: 12000, // max number of connections Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", ":6379") if err != nil { panic(err.Error()) } return c, err }, } }var pool = newPool()func main() { c := pool.Get() defer c.Close() test,_:=c.Do("HGETALL", "test:1") fmt.Println(test)}
If for example you want to reuse a pool inside another function you do it like this:
func test() { c := pool.Get() defer c.Close() test2,_:=c.Do("HGETALL", "test:2") fmt.Println(test2)}
###########
https://github.com/garyburd/redigo/blob/ee4f539b48a64737dd7a991cdebf1cb367ddb7fb/redis/pool_test.go
############
https://github.com/garyburd/redigo/issues/104
https://github.com/garyburd/redigo/issues/114
- redigo长连接/复用
- Redigo--Go连接Redis
- Redigo--Go连接Redis
- Redigo--用池管理redis连接
- 长连接
- 长连接
- 长连接
- 长连接
- 长连接
- 长连接
- 长连接
- 长连接
- 长连接
- 长连接
- redigo 用户指南
- golang redigo 在高并发下的问题。连接池占满CPU
- 长连接&短连接
- 长连接短连接
- mybatis惯用jdbcType数据类型
- Android-聊天窗口实现
- Java抽象类简述
- Android学习路线
- abdroid开发搭建
- redigo长连接/复用
- SYN标志
- [java]_[Collections.sort()实现List排序]
- JAF框架及在JavaMail中的应用
- 快速幂算法代码
- KMP快速字符串匹配 (next数组优化)
- hdu5616 Jam's balance
- 17228 真.狼人游戏
- iOS字体不清晰问题