Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedisStore Config #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions configtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ type StoreConfig struct {
Servers []string
GcProbability int
GcDivisor int
Password string
NetWork string
DB int
}
30 changes: 22 additions & 8 deletions redisstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -71,20 +79,26 @@ 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
}

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 {
Expand Down
3 changes: 3 additions & 0 deletions redisstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down