From a58c8180e87591e67e45717df65f842e7c99e0d0 Mon Sep 17 00:00:00 2001 From: "weizili.laptop" Date: Sun, 7 Jun 2015 16:26:23 +0800 Subject: [PATCH] Execute AUTH command when the "password" is configured --- cache/redis/redis.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 0e07eaed..ba543f61 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -51,6 +51,7 @@ type RedisCache struct { conninfo string dbNum int key string + password string } // create new redis cache with default collection name. @@ -149,16 +150,20 @@ func (rc *RedisCache) StartAndGC(config string) error { if _, ok := cf["key"]; !ok { cf["key"] = DefaultKey } - if _, ok := cf["conn"]; !ok { return errors.New("config has no conn key") } if _, ok := cf["dbNum"]; !ok { cf["dbNum"] = "0" } + if _, ok := cf["password"]; !ok { + cf["password"] = "" + } rc.key = cf["key"] rc.conninfo = cf["conn"] rc.dbNum, _ = strconv.Atoi(cf["dbNum"]) + rc.password = cf["password"] + rc.connectInit() c := rc.p.Get() @@ -171,6 +176,17 @@ func (rc *RedisCache) StartAndGC(config string) error { func (rc *RedisCache) connectInit() { dialFunc := func() (c redis.Conn, err error) { 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) if selecterr != nil { c.Close()