forked from hanguofeng/gocaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisstore.go
144 lines (120 loc) · 3.04 KB
/
redisstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package gocaptcha
import (
"bytes"
"crypto/md5"
"encoding/gob"
"encoding/hex"
"fmt"
"log"
"strconv"
"strings"
"time"
"gopkg.in/redis.v2"
)
func init() {
RegisterStore(storeName, CreateCaptchaRedisStore)
}
const (
captchaKeyFormat = "captcha:text:%s;rand:%s;time:%x;"
storeName = "redis"
)
type CaptchaRedisStore struct {
lifeTime time.Duration
stg *redis.Client
}
func CreateCaptchaRedisStore(config *StoreConfig) (StoreInterface, error) {
lifeTime := config.LifeTime
if config.Servers == nil || len(config.Servers) == 0 {
return nil, fmt.Errorf("servers must not be empty")
}
fullAddr := strings.TrimPrefix(config.Servers[0], "redis://")
pieces := strings.SplitN(fullAddr, "/", 2)
db := 0
addr := pieces[0]
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
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
}
func (this *CaptchaRedisStore) Get(key string) *CaptchaInfo {
s, err := this.stg.Get(key).Result()
if err != nil {
log.Printf("get key in redis error:%s", err)
return nil
}
captcha := CaptchaInfo{}
this.decodeCaptachInfo([]byte(s), &captcha)
return &captcha
}
func (this *CaptchaRedisStore) Add(captcha *CaptchaInfo) string {
key := fmt.Sprintf(captchaKeyFormat, captcha.Text, randStr(20), captcha.CreateTime.Unix())
key = hex.EncodeToString(md5.New().Sum([]byte(key)))
key = key[:32]
val, err := this.encodeCaptchaInfo(captcha)
if err == nil {
_, 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 {
_, error := this.stg.SetEx(key, this.lifeTime, string(val)).Result()
if error != nil {
log.Printf("set key in redis error:%s", error)
return false
} else {
//log.Printf("set key in redis %s", result)
return true
}
} else {
return false
}
}
func (this *CaptchaRedisStore) Del(key string) {
this.stg.Del(key)
}
func (this *CaptchaRedisStore) Destroy() {
}
func (this *CaptchaRedisStore) OnConstruct() {
}
func (this *CaptchaRedisStore) OnDestruct() {
}
func (this *CaptchaRedisStore) encodeCaptchaInfo(captcha *CaptchaInfo) ([]byte, error) {
buf := new(bytes.Buffer)
encoder := gob.NewEncoder(buf)
err := encoder.Encode(captcha)
if err != nil {
log.Printf("encode captcha info error:%s", err)
return nil, err
}
return buf.Bytes(), nil
}
func (this *CaptchaRedisStore) decodeCaptachInfo(b []byte, ret *CaptchaInfo) {
buf := bytes.NewBuffer(b)
decoder := gob.NewDecoder(buf)
if err := decoder.Decode(ret); err != nil {
log.Printf("decode captcha info error:%s", err)
}
return
}