-
Notifications
You must be signed in to change notification settings - Fork 2
/
kv.go
273 lines (212 loc) · 5.95 KB
/
kv.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package redis
import (
"context"
"strconv"
"time"
"github.com/go-redis/redis"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"libs.altipla.consulting/errors"
)
type StringKV struct {
db *Database
key string
}
func (kv *StringKV) Set(ctx context.Context, value string) error {
return kv.db.Cmdable(ctx).Set(kv.key, value, 0).Err()
}
func (kv *StringKV) SetTTL(ctx context.Context, value string, ttl time.Duration) error {
return kv.db.Cmdable(ctx).Set(kv.key, value, ttl).Err()
}
func (kv *StringKV) Get(ctx context.Context) (string, error) {
result, err := kv.db.Cmdable(ctx).Get(kv.key).Result()
if err != nil {
if err == redis.Nil {
return "", ErrNoSuchEntity
}
return "", err
}
return result, nil
}
func (kv *StringKV) Exists(ctx context.Context) (bool, error) {
result, err := kv.db.Cmdable(ctx).Exists(kv.key).Result()
if err != nil {
return false, err
}
return result == 1, nil
}
func (kv *StringKV) Delete(ctx context.Context) error {
return kv.db.Cmdable(ctx).Del(kv.key).Err()
}
type Int32KV struct {
db *Database
key string
}
func (kv *Int32KV) Set(ctx context.Context, value int32) error {
return kv.db.Cmdable(ctx).Set(kv.key, value, 0).Err()
}
func (kv *Int32KV) SetTTL(ctx context.Context, value int32, ttl time.Duration) error {
return kv.db.Cmdable(ctx).Set(kv.key, value, ttl).Err()
}
func (kv *Int32KV) Get(ctx context.Context) (int32, error) {
result, err := kv.db.Cmdable(ctx).Get(kv.key).Int64()
if err != nil {
if err == redis.Nil {
return 0, ErrNoSuchEntity
}
return 0, err
}
return int32(result), nil
}
func (kv *Int32KV) Exists(ctx context.Context) (bool, error) {
result, err := kv.db.Cmdable(ctx).Exists(kv.key).Result()
if err != nil {
return false, err
}
return result == 1, nil
}
func (kv *Int32KV) Delete(ctx context.Context) error {
return kv.db.Cmdable(ctx).Del(kv.key).Err()
}
type Int64KV struct {
db *Database
key string
}
func (kv *Int64KV) Set(ctx context.Context, value int64) error {
return kv.db.Cmdable(ctx).Set(kv.key, value, 0).Err()
}
func (kv *Int64KV) SetTTL(ctx context.Context, value int64, ttl time.Duration) error {
return kv.db.Cmdable(ctx).Set(kv.key, value, ttl).Err()
}
func (kv *Int64KV) Get(ctx context.Context) (int64, error) {
result, err := kv.db.Cmdable(ctx).Get(kv.key).Int64()
if err != nil {
if err == redis.Nil {
return 0, ErrNoSuchEntity
}
return 0, err
}
return int64(result), nil
}
func (kv *Int64KV) Exists(ctx context.Context) (bool, error) {
result, err := kv.db.Cmdable(ctx).Exists(kv.key).Result()
if err != nil {
return false, err
}
return result == 1, nil
}
func (kv *Int64KV) Delete(ctx context.Context) error {
return kv.db.Cmdable(ctx).Del(kv.key).Err()
}
// ProtoKV interacts with a protobuf value key.
type ProtoKV struct {
db *Database
key string
}
// Set changes the value of the key.
func (kv *ProtoKV) Set(ctx context.Context, value proto.Message) error {
return kv.SetTTL(ctx, value, 0)
}
// SetTTL changes the value of the key with a Time-To-Live.
func (kv *ProtoKV) SetTTL(ctx context.Context, value proto.Message, ttl time.Duration) error {
encoded, err := protojson.Marshal(value)
if err != nil {
return errors.Trace(err)
}
return kv.db.Cmdable(ctx).Set(kv.key, encoded, ttl).Err()
}
// Get decodes the value in the provided message.
func (kv *ProtoKV) Get(ctx context.Context, value proto.Message) error {
result, err := kv.db.Cmdable(ctx).Get(kv.key).Result()
if err != nil {
if err == redis.Nil {
return ErrNoSuchEntity
}
return errors.Trace(err)
}
return unmarshalProto(result, value)
}
// Exists checks if the key exists previously.
func (kv *ProtoKV) Exists(ctx context.Context) (bool, error) {
result, err := kv.db.Cmdable(ctx).Exists(kv.key).Result()
if err != nil {
return false, err
}
return result == 1, nil
}
// Delete the key.
func (kv *ProtoKV) Delete(ctx context.Context) error {
return kv.db.Cmdable(ctx).Del(kv.key).Err()
}
type BooleanKV struct {
db *Database
key string
}
func (kv *BooleanKV) Set(ctx context.Context, value bool) error {
return kv.db.Cmdable(ctx).Set(kv.key, value, 0).Err()
}
func (kv *BooleanKV) SetTTL(ctx context.Context, value bool, ttl time.Duration) error {
return kv.db.Cmdable(ctx).Set(kv.key, value, ttl).Err()
}
func (kv *BooleanKV) Get(ctx context.Context) (bool, error) {
result, err := kv.db.Cmdable(ctx).Get(kv.key).Result()
if err != nil {
if err == redis.Nil {
return false, ErrNoSuchEntity
}
return false, err
}
return strconv.ParseBool(result)
}
func (kv *BooleanKV) Exists(ctx context.Context) (bool, error) {
result, err := kv.db.Cmdable(ctx).Exists(kv.key).Result()
if err != nil {
return false, err
}
return result == 1, nil
}
func (kv *BooleanKV) Delete(ctx context.Context) error {
return kv.db.Cmdable(ctx).Del(kv.key).Err()
}
type TimeKV struct {
db *Database
key string
}
func (kv *TimeKV) Set(ctx context.Context, value time.Time) error {
rawValue, err := value.MarshalText()
if err != nil {
return errors.Trace(err)
}
return kv.db.Cmdable(ctx).Set(kv.key, string(rawValue), 0).Err()
}
func (kv *TimeKV) SetTTL(ctx context.Context, value time.Time, ttl time.Duration) error {
rawValue, err := value.MarshalText()
if err != nil {
return errors.Trace(err)
}
return kv.db.Cmdable(ctx).Set(kv.key, string(rawValue), ttl).Err()
}
func (kv *TimeKV) Get(ctx context.Context) (time.Time, error) {
rawResult, err := kv.db.Cmdable(ctx).Get(kv.key).Result()
if err != nil {
if err == redis.Nil {
return time.Time{}, ErrNoSuchEntity
}
return time.Time{}, err
}
result := time.Time{}
if err := result.UnmarshalText([]byte(rawResult)); err != nil {
return time.Time{}, err
}
return result, nil
}
func (kv *TimeKV) Exists(ctx context.Context) (bool, error) {
result, err := kv.db.Cmdable(ctx).Exists(kv.key).Result()
if err != nil {
return false, err
}
return result == 1, nil
}
func (kv *TimeKV) Delete(ctx context.Context) error {
return kv.db.Cmdable(ctx).Del(kv.key).Err()
}