-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.go
More file actions
134 lines (115 loc) · 2.53 KB
/
Copy pathstore.go
File metadata and controls
134 lines (115 loc) · 2.53 KB
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
package captcha
import (
"context"
"fmt"
"sync"
"time"
"github.com/redis/go-redis/v9"
)
var (
MemoryStoreMode = NewMemoryStore(180)
)
type Storer interface {
Save(id string, cap Captcha) error
Get(id string) (string, error)
Verify(id, code string) bool
Remove(id string) error
}
type MemoryStore struct {
sync.Mutex
Value map[string]Captcha
Expiration time.Duration
Total int
}
func NewMemoryStore(expire time.Duration) Storer {
m := new(MemoryStore)
m.Value = make(map[string]Captcha)
m.Expiration = expire
m.Total = 0
return m
}
func (m *MemoryStore) Save(id string, captcha Captcha) error {
m.Lock()
defer m.Unlock()
m.Value[id] = captcha
m.Total++
return nil
}
func (m *MemoryStore) Get(id string) (string, error) {
m.Lock()
defer m.Unlock()
if _, ok := m.Value[id]; !ok {
return "", fmt.Errorf("the %s not existing", id)
}
//计算保存时间是否已经超时
now := time.Now()
if now.Sub(m.Value[id].StartTime) > m.Expiration {
delete(m.Value, id)
return "", fmt.Errorf("the %s is expired", id)
}
answer := m.Value[id].Code
return answer, nil
}
func (m *MemoryStore) Verify(id, code string) bool {
if id == "" || code == "" {
return false
}
v, err := m.Get(id)
if err != nil {
return false
}
return v != "" && v == code
}
func (m *MemoryStore) Remove(id string) error {
delete(m.Value, id)
return nil
}
type RedisStore struct {
Client *redis.Client
Expiration time.Duration
}
var ctx = context.Background()
func NewRedisStore(host, password, port string, db int, expire time.Duration) Storer {
addr := fmt.Sprintf("%s:%s", host, port)
rdb := redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: db,
})
r := new(RedisStore)
r.Client = rdb
r.Expiration = expire
return r
}
func (r *RedisStore) Get(id string) (string, error) {
res, err := r.Client.Get(ctx, id).Result()
if err != nil {
return "", fmt.Errorf("redis get %s error: %w", id, err)
}
return res, nil
}
func (r *RedisStore) Save(id string, cap Captcha) error {
_, err := r.Client.SetNX(ctx, id, cap, 300).Result()
if err != nil {
return fmt.Errorf("redis set %s error: %w", id, err)
}
return nil
}
func (r *RedisStore) Verify(id, code string) bool {
if id == "" || code == "" {
return false
}
v, err := r.Get(id)
if err != nil {
fmt.Println("Err: ", err)
return false
}
return v != "" && v == code
}
func (r *RedisStore) Remove(id string) error {
_, err := r.Client.Del(ctx, id).Result()
if err != nil {
return fmt.Errorf("redis del %s error: %w", id, err)
}
return nil
}