a58c8180 by weizili.laptop

Execute AUTH command when the "password" is configured

1 parent af71289c
...@@ -51,6 +51,7 @@ type RedisCache struct { ...@@ -51,6 +51,7 @@ type RedisCache struct {
51 conninfo string 51 conninfo string
52 dbNum int 52 dbNum int
53 key string 53 key string
54 password string
54 } 55 }
55 56
56 // create new redis cache with default collection name. 57 // create new redis cache with default collection name.
...@@ -149,16 +150,20 @@ func (rc *RedisCache) StartAndGC(config string) error { ...@@ -149,16 +150,20 @@ func (rc *RedisCache) StartAndGC(config string) error {
149 if _, ok := cf["key"]; !ok { 150 if _, ok := cf["key"]; !ok {
150 cf["key"] = DefaultKey 151 cf["key"] = DefaultKey
151 } 152 }
152
153 if _, ok := cf["conn"]; !ok { 153 if _, ok := cf["conn"]; !ok {
154 return errors.New("config has no conn key") 154 return errors.New("config has no conn key")
155 } 155 }
156 if _, ok := cf["dbNum"]; !ok { 156 if _, ok := cf["dbNum"]; !ok {
157 cf["dbNum"] = "0" 157 cf["dbNum"] = "0"
158 } 158 }
159 if _, ok := cf["password"]; !ok {
160 cf["password"] = ""
161 }
159 rc.key = cf["key"] 162 rc.key = cf["key"]
160 rc.conninfo = cf["conn"] 163 rc.conninfo = cf["conn"]
161 rc.dbNum, _ = strconv.Atoi(cf["dbNum"]) 164 rc.dbNum, _ = strconv.Atoi(cf["dbNum"])
165 rc.password = cf["password"]
166
162 rc.connectInit() 167 rc.connectInit()
163 168
164 c := rc.p.Get() 169 c := rc.p.Get()
...@@ -171,6 +176,17 @@ func (rc *RedisCache) StartAndGC(config string) error { ...@@ -171,6 +176,17 @@ func (rc *RedisCache) StartAndGC(config string) error {
171 func (rc *RedisCache) connectInit() { 176 func (rc *RedisCache) connectInit() {
172 dialFunc := func() (c redis.Conn, err error) { 177 dialFunc := func() (c redis.Conn, err error) {
173 c, err = redis.Dial("tcp", rc.conninfo) 178 c, err = redis.Dial("tcp", rc.conninfo)
179 if err != nil {
180 return nil, err
181 }
182
183 if rc.password != "" {
184 if _, err := c.Do("AUTH", rc.password); err != nil {
185 c.Close()
186 return nil, err
187 }
188 }
189
174 _, selecterr := c.Do("SELECT", rc.dbNum) 190 _, selecterr := c.Do("SELECT", rc.dbNum)
175 if selecterr != nil { 191 if selecterr != nil {
176 c.Close() 192 c.Close()
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!