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