-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_hash.go
231 lines (211 loc) · 5.67 KB
/
single_hash.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package redis_warpper
import (
"context"
"errors"
"github.com/go-redis/redis/v8"
)
//Hash 存储多个对象,Hash本身有个计数字段,key+$$index
//每个对象,有自己的一个Record字段,其它字段该咋存就咋存
const (
hashIndexSuffix = "$$index"
hashObjectSuffix = "$$object"
)
type SingleHash struct {
rds *redis.Client
key string
}
func NewSingleHash(rds *redis.Client, key string) *SingleHash {
return &SingleHash{
rds: rds,
key: key,
}
}
func (l *SingleHash) getHashIndexKey() string {
return l.key + hashIndexSuffix
}
func (l *SingleHash) getObjectIndexKey(id string) string {
return id + hashObjectSuffix
}
func (l *SingleHash) getObjectFieldKey(id, field string) string {
return id + "#" + field
}
func (l *SingleHash) Size() (int64, error) {
r, err := l.rds.HGet(context.Background(), l.key, l.getHashIndexKey()).Int64()
if err != nil && !errors.Is(err, redis.Nil) {
return 0, err
}
if errors.Is(redis.Nil, err) {
return 0, nil
}
return r, nil
}
func (l *SingleHash) Del(obj interface{}, id ...string) error {
ctx := context.Background()
for _, d := range id {
fieldNames := getStructFieldName(obj)
fullField := make([]string, len(fieldNames)+1)
for idx, n := range fieldNames {
fullField[idx] = l.getObjectFieldKey(d, n)
}
//最后加入对象的Index字段
fullField[len(fullField)-1] = l.getObjectIndexKey(d)
//原子操作,确保削减数量
// _, err := l.rds.HDel(ctx, l.key, l.getHashIndexKey()).Result()
_, err := l.rds.HIncrBy(ctx, l.key, l.getHashIndexKey(), -1).Result()
if err != nil {
return err
}
l.rds.HDel(ctx, l.key, fullField...)
}
return nil
}
func (l *SingleHash) Set(id string, data interface{}) error {
if !requiredStruct(data) {
return ErrDataMustStruct
}
ctx := context.Background()
exist, err := l.rds.HExists(ctx, l.key, l.getObjectIndexKey(id)).Result()
if err != nil {
return err
}
//重新整理field
smap, err := structToMap(data)
dmap := map[string]interface{}{}
if err != nil {
return nil
}
dmap[l.getObjectIndexKey(id)] = id
for k, v := range smap {
dmap[l.getObjectFieldKey(id, k)] = v
}
//没有采用HSet批量设置,是要兼容Pika。HMSet 主流会被丢弃,Pika不支持HSet设置多个值
pipe := l.rds.Pipeline()
for k, v := range dmap {
pipe.HSet(ctx, l.key, k, v)
}
_, err = pipe.Exec(ctx)
if err != nil {
return err
}
if !exist {
l.rds.HIncrBy(ctx, l.key, l.getHashIndexKey(), 1)
}
return err
}
func (l *SingleHash) SetField(id, field string, data interface{}) error {
ctx := context.Background()
dmap := map[string]interface{}{}
dmap[l.getObjectFieldKey(id, field)] = data
dmap[l.getObjectIndexKey(id)] = id //增加固定field
//没有采用HSet批量设置,是要兼容Pika。HMSet 主流会被丢弃,Pika不支持HSet设置多个值
pipe := l.rds.Pipeline()
for k, v := range dmap {
pipe.HSet(ctx, l.key, k, v)
}
_, err := pipe.Exec(ctx)
if err != nil {
return err
}
return err
}
func (l *SingleHash) Incr(id, field string, incr int64) error {
ctx := context.Background()
pipe := l.rds.Pipeline()
//确保ID的记录存在
pipe.HSet(ctx, l.key, l.getObjectIndexKey(id), id)
pipe.HIncrBy(ctx, l.key, l.getObjectFieldKey(id, field), incr).Result()
_, err := pipe.Exec(ctx)
return err
}
func (l *SingleHash) GetField(id, field string) (string, error) {
ctx := context.Background()
ex, err := l.Contains(id)
if err != nil {
return "", err
}
if !ex {
return "", ErrKeyNotExist
}
r, err := l.rds.HGet(ctx, l.key, l.getObjectFieldKey(id, field)).Result()
if err != nil {
return "", err
}
return r, nil
}
func (l *SingleHash) Contains(id string) (bool, error) {
ctx := context.Background()
r, err := l.rds.HExists(ctx, l.key, l.getObjectIndexKey(id)).Result()
return r, err
}
func (l *SingleHash) Scan(modal interface{}, id string) error {
if !requiredStruct(modal) {
return ErrDataMustStruct
}
ctx := context.Background()
s, err := l.rds.HExists(ctx, l.key, l.getObjectIndexKey(id)).Result()
if err != nil {
return err
}
if !s {
//指定ID的数据不存在
return ErrKeyNotExist
}
fieldNames := getStructFieldName(modal)
//重新整理字段名称为存储用到的完整字段名(id+fieldname)
hashFields := make([]string, len(fieldNames))
for i := 0; i < len(fieldNames); i++ {
hashFields[i] = l.getObjectFieldKey(id, fieldNames[i])
}
vals, err := l.rds.HMGet(ctx, l.key, hashFields...).Result()
if err != nil {
return err
}
dmap := map[string]interface{}{}
for i := 0; i < len(fieldNames); i++ {
dmap[fieldNames[i]] = vals[i]
}
err = scanMapToStruct(modal, dmap)
if err != nil {
return err
}
return nil
}
func (l *SingleHash) Range(modal interface{}, id ...string) (map[string]interface{}, error) {
if !requiredStruct(modal) {
return nil, ErrDataMustStruct
}
ctx := context.Background()
ret := map[string]interface{}{}
for _, d := range id {
s, err := l.rds.HExists(ctx, l.key, l.getObjectIndexKey(d)).Result()
if err != nil {
return nil, err
}
if !s {
//指定ID的数据不存在
continue
}
inst := newModalInst(modal)
//解析出字段值,根据字段值查找Field,再赋值
fieldNames := getStructFieldName(inst)
//重新整理字段名称为存储用到的完整字段名(id+fieldname)
hashFields := make([]string, len(fieldNames))
for i := 0; i < len(fieldNames); i++ {
hashFields[i] = l.getObjectFieldKey(d, fieldNames[i])
}
vals, err := l.rds.HMGet(ctx, l.key, hashFields...).Result()
if err != nil {
return nil, err
}
dmap := map[string]interface{}{}
for i := 0; i < len(fieldNames); i++ {
dmap[fieldNames[i]] = vals[i]
}
err = scanMapToStruct(inst, dmap)
if err != nil {
return nil, err
}
ret[d] = inst
}
return ret, nil
}