redigo长连接/复用
2016-02-01 00:56
441 查看
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):
If for example you want to reuse a pool inside another function you do it like this:
###########
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
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
相关文章推荐
- Go将统治下一个十年
- paper4—Multi-MPE_Trigger_Algorithm—temp
- AlphaGo:黑色方碑?
- paper4—Multi-MPE_Trigger_Algorithm—testing
- paper3—Algorithm1-Engine-Trigger-on-FCD—temp
- paper3—Algorithm2-Engine-Trigger-on-SMD—temp
- Google Guava 类库简介
- 114DNS Public DNS+ 阿里DNS 百度DNS 360 DNS派 Google DNS
- pymongo简单用法
- Google大中华区总裁石博盟用中文拜年
- ZOJ-2855-Google Map【4th浙江省赛】
- LeetCode Algorithms #169 <Majority Element>
- LeetCode Algorithms #171 <Excel Sheet Column Number>
- lightoj1030 - Discovering Gold【概率dp求期望】
- LeetCode Algorithms #242 <Valid Anagram>
- POJ 3259/USACO 2006 December Gold:Wormholes
- LeetCode Algorithms #292 <Nim Game>
- 《Learn You Some Erlang for Great Good!》的学习笔记(四)
- projecteuler---->problem=9----Special Pythagorean triplet
- 【HDOJ】4775 Infinite Go