phone_check.go
1.0 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
package cache
import (
"encoding/json"
"time"
)
const phoneCheckExpire = 60 * 5
//短信验证码验证缓存
type PhoneCheckCache struct {
}
func (ca PhoneCheckCache) phoneCheckKey(smsCodeIdentity string) string {
str := KEY_PREFIX + "phone-check:" + smsCodeIdentity
return str
}
func (ca PhoneCheckCache) Add(smsCodeIdentity string, value interface{}) error {
key := ca.phoneCheckKey(smsCodeIdentity)
data, err := json.Marshal(value)
if err != nil {
return err
}
result := clientRedis.Set(key, string(data), time.Duration(phoneCheckExpire*time.Second))
return result.Err()
}
func (ca PhoneCheckCache) Get(smsCodeIdentity string, value interface{}) error {
key := ca.phoneCheckKey(smsCodeIdentity)
result := clientRedis.Get(key)
if result.Err() != nil {
return result.Err()
}
err := json.Unmarshal([]byte(result.Val()), value)
return err
}
type PhoneCheckItem struct {
Phone string `json:"phone"`
SmsCodeIdentity string `json:"smsCodeIdentity"`
Action int `json:"action"`
}