-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisStore.go
53 lines (45 loc) · 954 Bytes
/
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
package captcha
import (
"sync"
"github.com/go-redis/redis"
"time"
"fmt"
)
type redisStore struct {
sync.RWMutex
client *redis.Client
// 超时时间
expiration time.Duration
}
func NewRedisStore(addr string, password string, db int, expiration time.Duration) Store {
s := new(redisStore)
client := redis.NewClient(&redis.Options{
Addr: addr,
Password: password, // no password set
DB: db, // use default DB
})
pong, err := client.Ping().Result()
if err != nil {
panic(err)
} else {
fmt.Println(pong)
}
s.expiration = expiration
s.client = client
return s
}
func (s *redisStore) Set(id string, digits []byte) {
s.Lock()
err :=s.client.Set("captcha_"+id,string(digits),s.expiration).Err()
if err!=nil {
panic(err)
}
s.Unlock()
}
func (s *redisStore) Get(id string, clear bool)(digits []byte) {
val, err := s.client.Get("captcha_"+id).Result()
if err !=nil {
digits = []byte(val)
}
return
}