f988f035 by astaxie

redis provider for session and cache support select db

1 parent 1b4158c1
...@@ -32,6 +32,7 @@ package redis ...@@ -32,6 +32,7 @@ package redis
32 import ( 32 import (
33 "encoding/json" 33 "encoding/json"
34 "errors" 34 "errors"
35 "strconv"
35 "time" 36 "time"
36 37
37 "github.com/garyburd/redigo/redis" 38 "github.com/garyburd/redigo/redis"
...@@ -48,6 +49,7 @@ var ( ...@@ -48,6 +49,7 @@ var (
48 type RedisCache struct { 49 type RedisCache struct {
49 p *redis.Pool // redis connection pool 50 p *redis.Pool // redis connection pool
50 conninfo string 51 conninfo string
52 dbNum int
51 key string 53 key string
52 } 54 }
53 55
...@@ -137,7 +139,7 @@ func (rc *RedisCache) ClearAll() error { ...@@ -137,7 +139,7 @@ func (rc *RedisCache) ClearAll() error {
137 } 139 }
138 140
139 // start redis cache adapter. 141 // start redis cache adapter.
140 // config is like {"key":"collection key","conn":"connection info"} 142 // config is like {"key":"collection key","conn":"connection info","dbNum":"0"}
141 // the cache item in redis are stored forever, 143 // the cache item in redis are stored forever,
142 // so no gc operation. 144 // so no gc operation.
143 func (rc *RedisCache) StartAndGC(config string) error { 145 func (rc *RedisCache) StartAndGC(config string) error {
...@@ -151,9 +153,12 @@ func (rc *RedisCache) StartAndGC(config string) error { ...@@ -151,9 +153,12 @@ func (rc *RedisCache) StartAndGC(config string) error {
151 if _, ok := cf["conn"]; !ok { 153 if _, ok := cf["conn"]; !ok {
152 return errors.New("config has no conn key") 154 return errors.New("config has no conn key")
153 } 155 }
154 156 if _, ok := cf["dbNum"]; !ok {
157 cf["dbNum"] = "0"
158 }
155 rc.key = cf["key"] 159 rc.key = cf["key"]
156 rc.conninfo = cf["conn"] 160 rc.conninfo = cf["conn"]
161 rc.dbNum, _ = strconv.Atoi(cf["dbNum"])
157 rc.connectInit() 162 rc.connectInit()
158 163
159 c := rc.p.Get() 164 c := rc.p.Get()
...@@ -166,6 +171,11 @@ func (rc *RedisCache) StartAndGC(config string) error { ...@@ -166,6 +171,11 @@ func (rc *RedisCache) StartAndGC(config string) error {
166 func (rc *RedisCache) connectInit() { 171 func (rc *RedisCache) connectInit() {
167 dialFunc := func() (c redis.Conn, err error) { 172 dialFunc := func() (c redis.Conn, err error) {
168 c, err = redis.Dial("tcp", rc.conninfo) 173 c, err = redis.Dial("tcp", rc.conninfo)
174 _, selecterr := c.Do("SELECT", rc.dbNum)
175 if selecterr != nil {
176 c.Close()
177 return nil, selecterr
178 }
169 return 179 return
170 } 180 }
171 // initialize a new pool 181 // initialize a new pool
......
...@@ -118,12 +118,13 @@ type RedisProvider struct { ...@@ -118,12 +118,13 @@ type RedisProvider struct {
118 savePath string 118 savePath string
119 poolsize int 119 poolsize int
120 password string 120 password string
121 dbNum int
121 poollist *redis.Pool 122 poollist *redis.Pool
122 } 123 }
123 124
124 // init redis session 125 // init redis session
125 // savepath like redis server addr,pool size,password 126 // savepath like redis server addr,pool size,password,dbnum
126 // e.g. 127.0.0.1:6379,100,astaxie 127 // e.g. 127.0.0.1:6379,100,astaxie,0
127 func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error { 128 func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
128 rp.maxlifetime = maxlifetime 129 rp.maxlifetime = maxlifetime
129 configs := strings.Split(savePath, ",") 130 configs := strings.Split(savePath, ",")
...@@ -143,6 +144,16 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error { ...@@ -143,6 +144,16 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
143 if len(configs) > 2 { 144 if len(configs) > 2 {
144 rp.password = configs[2] 145 rp.password = configs[2]
145 } 146 }
147 if len(configs) > 3 {
148 dbnum, err := strconv.Atoi(configs[1])
149 if err != nil || dbnum < 0 {
150 rp.dbNum = 0
151 } else {
152 rp.dbNum = dbnum
153 }
154 } else {
155 rp.dbNum = 0
156 }
146 rp.poollist = redis.NewPool(func() (redis.Conn, error) { 157 rp.poollist = redis.NewPool(func() (redis.Conn, error) {
147 c, err := redis.Dial("tcp", rp.savePath) 158 c, err := redis.Dial("tcp", rp.savePath)
148 if err != nil { 159 if err != nil {
...@@ -154,6 +165,11 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error { ...@@ -154,6 +165,11 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
154 return nil, err 165 return nil, err
155 } 166 }
156 } 167 }
168 _, err = c.Do("SELECT", rp.dbNum)
169 if err != nil {
170 c.Close()
171 return nil, err
172 }
157 return c, err 173 return c, err
158 }, rp.poolsize) 174 }, rp.poolsize)
159 175
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!