1
0
mirror of https://gitea.com/xorm/cachestore synced 2025-10-05 15:52:47 +02:00

Merge pull request #1 from masiqi/master

为redis添加密码验证支持
This commit is contained in:
Hank Shen
2017-04-09 11:18:04 +08:00
committed by GitHub

View File

@@ -1,9 +1,9 @@
package cachestore
import (
"github.com/go-xorm/cachestore/redigo/redis"
"encoding/json"
"errors"
"github.com/go-xorm/cachestore/redigo/redis"
"log"
)
@@ -17,6 +17,7 @@ type RedisCache struct {
c redis.Conn
conninfo string
key string
password string
Debug bool
}
@@ -26,11 +27,16 @@ func NewRedisCache(cf map[string]string) *RedisCache {
if _, ok := cf["key"]; !ok {
cf["key"] = DefaultKey
}
if _, ok := cf["password"]; !ok {
cf["password"] = ""
}
rc.password = cf["password"]
rc.key = cf["key"]
rc.conninfo = cf["conn"]
var err error
rc.c, err = rc.connectInit()
if err != nil {
log.Println("[Redis]InitErr: ", err)
rc.c = nil
}
return rc
@@ -53,7 +59,9 @@ func (rc *RedisCache) Get(key string) (interface{}, error) {
return nil, err
}
var v interface{}
err = Decode(val.([]byte), &v)
x, _ := val.([]byte)
err = Decode(x, &v)
if err != nil {
if rc.Debug {
log.Println("[Redis]DecodeErr: ", err, "Key:", key)
@@ -211,5 +219,11 @@ func (rc *RedisCache) connectInit() (redis.Conn, error) {
if err != nil {
return nil, err
}
if rc.password != "" {
if _, err := c.Do("AUTH", rc.password); err != nil {
c.Close()
return nil, err
}
}
return c, nil
}