-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
67 lines (59 loc) · 1.72 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
package sessions
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"time"
)
type redisSession struct {
CookieValue string
ValueType valueType
Expire int
}
func newRedisSession(cookieValue string, valueType valueType, expire int) Session {
return &redisSession{
CookieValue: cookieValue,
ValueType: valueType,
Expire: expire,
}
}
func (r *redisSession) stringer() string {
return prefixValue + r.CookieValue
}
func (r *redisSession) Get(keys ...string) interface{} {
if len(keys) == 0 && r.ValueType == ValueMap {
panic("key can't nil")
}
if r.ValueType == ValueMap {
result, err := rdb.HGet(context.TODO(), r.stringer(), keys[0]).Result()
if err != nil && err != redis.Nil {
panic(fmt.Sprintf("hget value error:%s", err))
}
return result
}
result, err := rdb.Get(context.TODO(), r.stringer()).Result()
if err != nil && err != redis.Nil {
panic(fmt.Sprintf("get value error:%s", err))
}
return result
}
func (r *redisSession) Set(value interface{}, keys ...string) {
if r.ValueType == ValueMap && len(keys) == 0 {
panic("The type is map. Please pass a key")
} else if r.ValueType == ValueString && len(keys) > 0 {
panic("The type is string. Please don't pass the key")
}
if r.ValueType == ValueMap {
if err := rdb.HSet(context.TODO(), r.stringer(), keys[0], value).Err(); err != nil {
panic(fmt.Sprintf("hset value error:%s", err.Error()))
}
} else {
if err := rdb.Set(context.TODO(), r.stringer(), value, time.Duration(0)).Err(); err != nil {
panic(fmt.Sprintf("set value error:%s", err.Error()))
}
}
rdb.Expire(context.TODO(), r.stringer(), time.Second*time.Duration(r.Expire))
}
func (r *redisSession) Delete(keys ...string) {
rdb.Del(context.TODO(), keys...)
}