-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathReDiS_session.go
146 lines (122 loc) · 4.07 KB
/
ReDiS_session.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
141
142
143
144
145
146
package Rd
import (
"context"
"strings"
"github.com/kokizzu/gotro/D"
"github.com/kokizzu/gotro/I"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/M"
"github.com/kokizzu/gotro/S"
"github.com/redis/rueidis"
)
const DEFAULT_HOST = `127.0.0.1:6379`
type RedisSession struct {
Pool rueidis.Client
Prefix string
}
func (sess RedisSession) Product() string {
return D.REDIS
}
// TryRedisSession non panic version, returns error if failed to connect
func TryRedisSession(host, pass string, dbNum int, prefix string) (*RedisSession, error) {
host = S.IfEmpty(host, DEFAULT_HOST)
conn, err := rueidis.NewClient(rueidis.ClientOption{
InitAddress: []string{host},
Password: pass,
SelectDB: dbNum,
})
return &RedisSession{
Pool: conn,
Prefix: prefix,
}, err
}
// NewRedisSession panic version
func NewRedisSession(host, pass string, dbNum int, prefix string) *RedisSession {
host = S.IfEmpty(host, DEFAULT_HOST)
conn, err := rueidis.NewClient(rueidis.ClientOption{
InitAddress: []string{host},
Password: pass,
SelectDB: dbNum,
})
L.PanicIf(err, `redis.NewClient`)
return &RedisSession{
Pool: conn,
Prefix: prefix,
}
}
func (sess RedisSession) Del(key string) {
err := sess.Pool.Do(context.Background(), sess.Pool.B().Del().Key(sess.Prefix+key).Build()).Error()
L.IsError(err, `failed to DEL`, key)
}
// Expiry check the expiry time in second
func (sess RedisSession) Expiry(key string) int64 {
val, err := sess.Pool.Do(context.Background(), sess.Pool.B().Ttl().Key(sess.Prefix+key).Build()).AsInt64()
if err != nil {
return -1
}
return val
}
func (sess RedisSession) FadeStr(key, val string, sec int64) {
err := sess.Pool.Do(context.Background(), sess.Pool.B().Setex().Key(sess.Prefix+key).Seconds(sec).Value((val)).Build()).Error()
L.IsError(err, `failed to SETEX`, key, sec, val)
}
func (sess RedisSession) FadeInt(key string, val int64, sec int64) {
sess.FadeStr(key, I.ToS(val), sec)
}
func (sess RedisSession) FadeMSX(key string, val M.SX, sec int64) {
sess.FadeStr(key, M.ToJson(val), sec)
}
func (sess RedisSession) GetStr(key string) string {
val, err := sess.Pool.Do(context.Background(), sess.Pool.B().Get().Key(sess.Prefix+key).Build()).ToString()
if err != nil && !(err.Error() == `redis nil message` || err.Error() == `redis: nil`) {
L.IsError(err, `failed to GET`, key)
}
return strings.Trim(val, `"`) // remove quotes, khusus redis
}
func (sess RedisSession) GetInt(key string) int64 {
return S.ToI(sess.GetStr(key))
}
func (sess RedisSession) GetMSX(key string) M.SX {
return S.JsonToMap(sess.GetStr(key))
}
func (sess RedisSession) Inc(key string) int64 {
val, err := sess.Pool.Do(context.Background(), sess.Pool.B().Incr().Key(sess.Prefix+key).Build()).AsInt64()
L.IsError(err, `failed to INCR`, key)
return val
}
func (sess RedisSession) Dec(key string) int64 {
val, err := sess.Pool.Do(context.Background(), sess.Pool.B().Decr().Key(sess.Prefix+key).Build()).AsInt64()
L.IsError(err, `failed to DECR`, key)
return val
}
func (sess RedisSession) SetStr(key, val string) {
err := sess.Pool.Do(context.Background(), sess.Pool.B().Set().Key(sess.Prefix+key).Value(val).Build()).Error()
L.IsError(err, `failed to SET`, key, val)
}
func (sess RedisSession) SetInt(key string, val int64) {
sess.SetStr(key, I.ToS(val))
}
func (sess RedisSession) SetMSX(key string, val M.SX) {
sess.SetStr(key, val.ToJson())
}
func (sess RedisSession) SetMSS(key string, val M.SS) {
sess.SetStr(key, val.ToJson())
}
func (sess RedisSession) Lpush(key string, val string) {
sess.Pool.Do(context.Background(), sess.Pool.B().Lpush().Key(key).Element(val).Build())
}
func (sess RedisSession) Rpush(key string, val string) {
sess.Pool.Do(context.Background(), sess.Pool.B().Rpush().Key(key).Element(val).Build())
}
func (sess RedisSession) Lrange(key string, start, end int64) []string {
res, err := sess.Pool.Do(context.Background(), sess.Pool.B().Lrange().Key(key).Start(start).Stop(end).Build()).ToArray()
if err != nil {
return []string{}
}
str := make([]string, 0)
for _, v := range res {
v, _ := v.ToString()
str = append(str, v)
}
return str
}