1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 19:41:00 +00:00

Merge pull request #1193 from zieckey/auth

Execute AUTH command when the "password" is configured
This commit is contained in:
astaxie 2015-06-07 20:34:32 +08:00
commit 9280683935

18
cache/redis/redis.go vendored
View File

@ -51,6 +51,7 @@ type RedisCache struct {
conninfo string conninfo string
dbNum int dbNum int
key string key string
password string
} }
// create new redis cache with default collection name. // create new redis cache with default collection name.
@ -149,16 +150,20 @@ func (rc *RedisCache) StartAndGC(config string) error {
if _, ok := cf["key"]; !ok { if _, ok := cf["key"]; !ok {
cf["key"] = DefaultKey cf["key"] = DefaultKey
} }
if _, ok := cf["conn"]; !ok { if _, ok := cf["conn"]; !ok {
return errors.New("config has no conn key") return errors.New("config has no conn key")
} }
if _, ok := cf["dbNum"]; !ok { if _, ok := cf["dbNum"]; !ok {
cf["dbNum"] = "0" cf["dbNum"] = "0"
} }
if _, ok := cf["password"]; !ok {
cf["password"] = ""
}
rc.key = cf["key"] rc.key = cf["key"]
rc.conninfo = cf["conn"] rc.conninfo = cf["conn"]
rc.dbNum, _ = strconv.Atoi(cf["dbNum"]) rc.dbNum, _ = strconv.Atoi(cf["dbNum"])
rc.password = cf["password"]
rc.connectInit() rc.connectInit()
c := rc.p.Get() c := rc.p.Get()
@ -171,6 +176,17 @@ func (rc *RedisCache) StartAndGC(config string) error {
func (rc *RedisCache) connectInit() { func (rc *RedisCache) connectInit() {
dialFunc := func() (c redis.Conn, err error) { dialFunc := func() (c redis.Conn, err error) {
c, err = redis.Dial("tcp", rc.conninfo) c, err = redis.Dial("tcp", rc.conninfo)
if err != nil {
return nil, err
}
if rc.password != "" {
if _, err := c.Do("AUTH", rc.password); err != nil {
c.Close()
return nil, err
}
}
_, selecterr := c.Do("SELECT", rc.dbNum) _, selecterr := c.Do("SELECT", rc.dbNum)
if selecterr != nil { if selecterr != nil {
c.Close() c.Close()