-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
140 lines (135 loc) · 3.48 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package sess
import (
"context"
xerr "github.com/goclub/error"
red "github.com/goclub/redis"
"strconv"
"time"
)
func NewRedisStore(option RedisStoreOption) RedisStore {
return RedisStore{
option: option,
}
}
type RedisStoreOption struct {
Client red.Connecter
StoreKeyPrefix string
}
type RedisStore struct {
option RedisStoreOption
}
func (m RedisStore) getKey(storeKey string) (key string) {
return m.option.StoreKeyPrefix + ":" + storeKey
}
func (m RedisStore) InitSession(ctx context.Context, storeKey string, sessionTTL time.Duration) (err error) {
key := m.getKey(storeKey)
client := m.option.Client
// lua 保证原子性
// hset key __goclub_session_create_time time.Now() 是为了让 key 存在
script := `
local key = KEYS[1]
local field = KEYS[2]
local nowUnix = ARGV[1]
local ttl = ARGV[2]
redis.call("HSET", key, field, nowUnix)
return redis.call("pexpire", key, ttl)
`
field := "__goclub_session_create_time"
evalKeys := []string{key, field}
argv := []string{strconv.FormatInt(time.Now().Unix(), 10), strconv.FormatInt(sessionTTL.Milliseconds(), 10)}
reply, isNil, err := client.Eval(ctx, red.Script{
KEYS: evalKeys,
ARGV: argv,
Script: script,
})
if err != nil {
return
}
if isNil {
return xerr.New("goclub/session: RedisStore InitSession redis can not be nil")
}
intReply, err := reply.Int64()
if err != nil {
return
}
if intReply == 0 {
// 理论上 pexpire 不会返回 0 ,但是严谨一点应当在返回 0 时候返回错误
return xerr.New("goclub/session: RedisStore NewSession redis pexpire fail, key is " + key)
}
return
}
func (m RedisStore) StoreKeyExists(ctx context.Context, storeKey string) (existed bool, err error) {
key := m.getKey(storeKey)
client := m.option.Client
reply, err := red.EXISTS{
Key: key,
}.Do(ctx, client)
if err != nil {
return
}
existed = reply == 1
return
}
func (m RedisStore) StoreKeyRemainingTTL(ctx context.Context, storeKey string) (remainingTTL time.Duration, err error) {
key := m.getKey(storeKey)
client := m.option.Client
result, err := red.PTTL{
Key: key,
}.Do(ctx, client)
if err != nil {
return
}
return result.TTL, nil
}
func (m RedisStore) RenewTTL(ctx context.Context, storeKey string, ttl time.Duration) (err error) {
key := m.getKey(storeKey)
client := m.option.Client
_, err = red.PEXPIRE{
Key: key,
Duration: ttl,
}.Do(ctx, client)
if err != nil {
return
}
return
}
func (m RedisStore) Get(ctx context.Context, storeKey string, field string) (value string, hasValue bool, err error) {
key := m.getKey(storeKey)
client := m.option.Client
hasValue = true
value, isNil, err := client.DoStringReply(ctx, []string{"HGET", key, field})
if err != nil {
return
}
if isNil {
return "", false, nil
}
return
}
func (m RedisStore) Set(ctx context.Context, storeKey string, field string, value string) (err error) {
key := m.getKey(storeKey)
client := m.option.Client
_, err = client.DoIntegerReplyWithoutNil(ctx, []string{"HSET", key, field, value})
if err != nil {
return
}
return
}
func (m RedisStore) Delete(ctx context.Context, storeKey string, field string) (err error) {
key := m.getKey(storeKey)
client := m.option.Client
_, err = client.DoIntegerReplyWithoutNil(ctx, []string{"HDEL", key, field})
if err != nil {
return
}
return
}
func (m RedisStore) Destroy(ctx context.Context, storeKey string) (err error) {
key := m.getKey(storeKey)
client := m.option.Client
_, err = red.DEL{Key: key}.Do(ctx, client)
if err != nil {
return
}
return
}