login_qrcode.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
44
45
46
47
package cache
import (
"encoding/json"
"time"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
)
//二维码信息缓存
type LoginQrcodeCache struct {
}
func (lq LoginQrcodeCache) keyString(str string) string {
str1 := KEY_PREFIX + "login:qrcode:" + str
return str1
}
func (lq LoginQrcodeCache) Save(qrcode domain.QrcodeMessage) error {
nowTime := time.Now().Unix()
exp := qrcode.ExpiresAt - nowTime
if exp <= 0 {
exp = 60 * 60 * 2
}
key := lq.keyString(qrcode.Id)
bt, _ := json.Marshal(qrcode)
result := clientRedis.Set(key, string(bt), time.Duration(exp))
return result.Err()
}
func (lq LoginQrcodeCache) Remove(id string) error {
keyStr := lq.keyString(id)
result := clientRedis.Del(keyStr)
return result.Err()
}
func (lq LoginQrcodeCache) Get(id string) (*domain.QrcodeMessage, error) {
keyStr := lq.keyString(id)
result := clientRedis.Get(keyStr)
re, _ := result.Result()
var data domain.QrcodeMessage
err := json.Unmarshal([]byte(re), &data)
if err != nil {
return nil, err
}
return &data, err
}