forked from dchest/captcha
/
redis.go
120 lines (105 loc) · 2.78 KB
/
redis.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
package store
import (
"context"
"encoding/hex"
"time"
"github.com/go-redis/redis/v8"
)
// NewRedisStore create an instance of a redis store
func NewRedisStore(opts *redis.Options, expiration time.Duration, out Logger, prefix ...string) Store {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisStoreWithCli(
redis.NewClient(opts),
expiration,
out,
prefix...,
)
}
// NewRedisStoreWithCli create an instance of a redis store
func NewRedisStoreWithCli(cli *redis.Client, expiration time.Duration, out Logger, prefix ...string) Store {
store := &redisStore{
cli: cli,
expiration: expiration,
out: out,
}
if len(prefix) > 0 {
store.prefix = prefix[0]
}
return store
}
// NewRedisClusterStore create an instance of a redis cluster store
func NewRedisClusterStore(opts *redis.ClusterOptions, expiration time.Duration, out Logger, prefix ...string) Store {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisClusterStoreWithCli(
redis.NewClusterClient(opts),
expiration,
out,
prefix...,
)
}
// NewRedisClusterStoreWithCli create an instance of a redis cluster store
func NewRedisClusterStoreWithCli(cli *redis.ClusterClient, expiration time.Duration, out Logger, prefix ...string) Store {
store := &redisStore{
cli: cli,
expiration: expiration,
out: out,
}
if len(prefix) > 0 {
store.prefix = prefix[0]
}
return store
}
type clienter interface {
Get(ctx context.Context, key string) *redis.StringCmd
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
Del(ctx context.Context, keys ...string) *redis.IntCmd
}
type redisStore struct {
cli clienter
prefix string
out Logger
expiration time.Duration
}
func (s *redisStore) getKey(id string) string {
return s.prefix + id
}
func (s *redisStore) printf(format string, args ...interface{}) {
if s.out != nil {
s.out.Printf(format, args...)
}
}
func (s *redisStore) Set(id string, digits []byte) {
cmd := s.cli.Set(context.Background(), s.getKey(id), hex.EncodeToString(digits), s.expiration)
if err := cmd.Err(); err != nil {
s.printf("redis execution set command error: %s", err.Error())
}
return
}
func (s *redisStore) Get(id string, clear bool) []byte {
key := s.getKey(id)
cmd := s.cli.Get(context.Background(), key)
if err := cmd.Err(); err != nil {
if err == redis.Nil {
return nil
}
s.printf("redis execution get command error: %s", err.Error())
return nil
}
b, err := hex.DecodeString(cmd.Val())
if err != nil {
s.printf("hex decoding error: %s", err.Error())
return nil
}
if clear {
cmd := s.cli.Del(context.Background(), key)
if err := cmd.Err(); err != nil {
s.printf("redis execution del command error: %s", err.Error())
return nil
}
}
return b
}