diff --git a/configtypes.go b/configtypes.go index 334b0eb..a1485d6 100644 --- a/configtypes.go +++ b/configtypes.go @@ -71,4 +71,7 @@ type StoreConfig struct { Servers []string GcProbability int GcDivisor int + Password string + NetWork string + DB int } diff --git a/redisstore.go b/redisstore.go index 8424a1c..bbf7a69 100644 --- a/redisstore.go +++ b/redisstore.go @@ -42,13 +42,21 @@ func CreateCaptchaRedisStore(config *StoreConfig) (StoreInterface, error) { if len(pieces) == 2 { db, _ = strconv.Atoi(pieces[1]) } - + if config.DB > 0 { + db = config.DB + } opt := redis.Options{} opt.Addr = addr opt.DB = int64(db) opt.PoolSize = 0 - stg := redis.NewTCPClient(&opt) - + if len(config.Password)>0{ + opt.Password = config.Password + } + opt.Network = "tcp" + if len(config.NetWork)>0{ + opt.Network = config.NetWork + } + stg := redis.NewClient(&opt) return &CaptchaRedisStore{lifeTime, stg}, nil } @@ -71,9 +79,13 @@ func (this *CaptchaRedisStore) Add(captcha *CaptchaInfo) string { val, err := this.encodeCaptchaInfo(captcha) if err == nil { - if seterr := this.stg.SetEx(key, this.lifeTime, string(val)); seterr != nil { - log.Printf("add key in redis error:%s", seterr) + result, error :=this.stg.SetEx(key, this.lifeTime, string(val)).Result() + if error != nil { + log.Printf("add key in redis error:%s", error) + }else{ + log.Printf("add key in redis %s", result) } + } return key } @@ -81,10 +93,12 @@ func (this *CaptchaRedisStore) Add(captcha *CaptchaInfo) string { func (this *CaptchaRedisStore) Update(key string, captcha *CaptchaInfo) bool { val, err := this.encodeCaptchaInfo(captcha) if err == nil { - if seterr := this.stg.Set(key, string(val)); seterr != nil { - log.Printf("set key in redis error:%s", seterr) + result, error :=this.stg.Set(key, string(val)).Result() + if error != nil { + log.Printf("set key in redis error:%s", error) return false - } else { + }else{ + log.Printf("set key in redis %s", result) return true } } else { diff --git a/redisstore_test.go b/redisstore_test.go index 79d4f4e..c79dfce 100644 --- a/redisstore_test.go +++ b/redisstore_test.go @@ -14,6 +14,9 @@ func TestRedisStore(t *testing.T) { storeConfig.CaptchaConfig.LifeTime = time.Second * 100 storeConfig.Engine = "redis" storeConfig.Servers = []string{"127.0.0.1:6379"} + storeConfig.Password = "123456" + storeConfig.DB = 5 + storeConfig.NetWork = "tcp" store, _ := CreateCaptchaRedisStore(storeConfig) captcha := new(CaptchaInfo)