-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis_base.go
141 lines (114 loc) · 3.21 KB
/
redis_base.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
package svc
import (
"github.com/goinbox/redis"
"encoding/json"
"errors"
"reflect"
)
const (
ENTITY_REDIS_HASH_FIELD_TAG = "redis"
)
var LogKindRedisBaseSvc = []byte("RedisBaseSvc")
type RedisBaseSvc struct {
*BaseSvc
Rclient *redis.Client
}
func NewRedisBaseSvc(bs *BaseSvc, rclient *redis.Client) *RedisBaseSvc {
return &RedisBaseSvc{
BaseSvc: bs,
Rclient: rclient,
}
}
func (r *RedisBaseSvc) SaveJsonDataToRedis(key string, v interface{}, expireSeconds int64) error {
jsonBytes, err := json.Marshal(v)
if err != nil {
r.WarningLog(LogKindRedisBaseSvc, []byte("json_encode "+key+" error: "+err.Error()))
return err
}
err = r.Rclient.Do("set", key, string(jsonBytes), "ex", expireSeconds).Err
if err != nil {
r.Rclient.Free()
r.WarningLog(LogKindRedisBaseSvc, []byte("set "+key+" to redis error: "+err.Error()))
return err
}
return nil
}
func (r *RedisBaseSvc) GetJsonDataFromRedis(key string, v interface{}) (bool, error) {
reply := r.Rclient.Do("get", key)
if reply.Err != nil {
r.Rclient.Free()
r.WarningLog(LogKindRedisBaseSvc, []byte("get "+key+" from redis error: "+reply.Err.Error()))
return false, reply.Err
}
if reply == nil {
return false, nil
}
jsonBytes, err := reply.Bytes()
if err != nil {
r.WarningLog(LogKindRedisBaseSvc, []byte("reply "+key+" from redis error: "+err.Error()))
return false, err
}
err = json.Unmarshal(jsonBytes, v)
if err != nil {
r.WarningLog(LogKindRedisBaseSvc, []byte("json_decode "+key+" from redis error: "+err.Error()))
return false, err
}
return true, nil
}
func (r *RedisBaseSvc) SaveHashEntityToRedis(key string, entityPtr interface{}, expireSeconds int64) error {
eargs := r.reflectSaveHashEntityArgs(reflect.ValueOf(entityPtr).Elem())
args := make([]interface{}, len(eargs)+1)
args[0] = key
for i, arg := range eargs {
args[i+1] = arg
}
r.Rclient.Send("hmset", args...)
if expireSeconds > 0 {
r.Rclient.Send("expire", key, expireSeconds)
}
replies, errIndexes := r.Rclient.ExecPipelining()
if len(errIndexes) != 0 {
r.Rclient.Free()
msg := "hmset " + key + " to redis error:"
for _, i := range errIndexes {
msg += " " + replies[i].Err.Error()
}
r.WarningLog(LogKindRedisBaseSvc, []byte(msg))
return errors.New(msg)
}
return nil
}
func (r *RedisBaseSvc) GetHashEntityFromRedis(key string, entityPtr interface{}) (bool, error) {
reply := r.Rclient.Do("hgetall", key)
if reply.Err != nil {
r.Rclient.Free()
r.WarningLog(LogKindRedisBaseSvc, []byte("hgetall "+key+" from redis error: "+reply.Err.Error()))
return false, reply.Err
}
if reply.ArrReplyIsNil() {
return false, nil
}
err := reply.Struct(entityPtr)
if err != nil {
r.WarningLog(LogKindRedisBaseSvc, []byte("reply to struct "+key+" from redis error: "+err.Error()))
return false, err
}
return true, nil
}
func (r *RedisBaseSvc) reflectSaveHashEntityArgs(rev reflect.Value) []interface{} {
var args []interface{}
ret := rev.Type()
for i := 0; i < rev.NumField(); i++ {
revf := rev.Field(i)
if revf.Kind() == reflect.Struct {
args = r.reflectSaveHashEntityArgs(revf)
continue
}
retf := ret.Field(i)
fn, ok := retf.Tag.Lookup(ENTITY_REDIS_HASH_FIELD_TAG)
if ok {
args = append(args, fn, revf.Interface())
}
}
return args
}