-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
320 lines (262 loc) · 9.32 KB
/
storage.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package redistorage
import (
"fmt"
"time"
"github.com/go-redis/redis"
log "github.com/sirupsen/logrus"
)
// Storage facilitates streaming to redis
type Storage struct {
Client *redis.Client
Receiver chan redis.Message
}
// New create a new redis cache connection
func New(host, password string) *Storage {
return &Storage{
Client: createRedisClient(host, password),
Receiver: make(chan redis.Message, 0),
}
}
// Exists checks if keys exist and returns count of existing
func (s *Storage) Exists(keys ...string) (int64, error) {
return s.Client.Exists(keys...).Result()
}
// Duplicate creates a copy of a key
func (s *Storage) Duplicate(key string, newKey string) error {
return s.Client.Eval(
`return redis.call('SET', KEYS[2], redis.call('GET', KEYS[1]))`,
[]string{key, newKey},
).Err()
}
// Count counts elements following a pattern
func (s *Storage) Count(pattern string) (int64, error) {
r, err := s.Client.Eval(
`return #redis.pcall('keys', KEYS[1])`,
[]string{pattern},
).Result()
if err != nil {
return 0, err
}
return r.(int64), nil
}
// KeyExists checks if single key exists
func (s *Storage) KeyExists(key string) (bool, error) {
var count int64
count, err := s.Client.Exists(key).Result()
return count == 1, err
}
// Expire expires a key after duration
func (s *Storage) Expire(key string, after time.Duration) error {
return s.Client.Expire(key, after).Err()
}
// GetKeys gets all keys for pattern
func (s *Storage) GetKeys(pattern string) ([]string, error) {
return s.Client.Keys(pattern).Result()
}
// Set a value at key
func (s *Storage) Set(key string, value interface{}, expiration time.Duration) error {
return s.Client.Set(key, value, expiration).Err()
}
// Get a value at key
func (s *Storage) Get(key string) (string, error) {
return s.Client.Get(key).Result()
}
// GetStruct gets a value at key as struct
// obj needs to follow UnmarshalBinary interface
func (s *Storage) GetStruct(key string, obj interface{}) error {
return s.Client.Get(key).Scan(obj)
}
// Remove deletes keys and values
func (s *Storage) Remove(keys ...string) (int64, error) {
return s.Client.Del(keys...).Result()
}
// Rename renames a key to another
func (s *Storage) Rename(key, newKey string) error {
return s.Client.Rename(key, newKey).Err()
}
// SetIfNotExists only sets if it doesn't exist
func (s *Storage) SetIfNotExists(key string, value interface{}, expiration time.Duration) (bool, error) {
return s.Client.SetNX(key, value, expiration).Result()
}
// GetInt gets value as int
func (s *Storage) GetInt(key string) (int64, error) {
return s.Client.Get(key).Int64()
}
// Hashmaps
// SetMap sets fields of a redis hashmap
func (s *Storage) SetMap(key string, fields map[string]interface{}) error {
return s.Client.HMSet(key, fields).Err()
}
// SetMapField sets a field of a redis hashmap
func (s *Storage) SetMapField(key string, field string, value string) error {
return s.Client.HSet(key, field, value).Err()
}
// GetMap gets all fields of a redis hashmap
func (s *Storage) GetMap(key string) (map[string]string, error) {
return s.Client.HGetAll(key).Result()
}
// GetField gets field of a redis hashmap
func (s *Storage) GetField(key string, field string) (string, error) {
return s.Client.HGet(key, field).Result()
}
// GetMapFields gets fields from map
func (s *Storage) GetMapFields(key string, fields ...string) ([]interface{}, error) {
return s.Client.HMGet(key, fields...).Result()
}
// IncrementMapField Increments a hash maps field by value
func (s *Storage) IncrementMapField(key string, field string, inc int) error {
return s.Client.HIncrBy(key, field, int64(inc)).Err()
}
// Lists
// ListLen returns a list length
func (s *Storage) ListLen(key string) (int64, error) {
return s.Client.LLen(key).Result()
}
// ListGet returns list element at index
func (s *Storage) ListGet(key string, index int64) (string, error) {
return s.Client.LIndex(key, index).Result()
}
// ListPush appends to list at key
func (s *Storage) ListPush(key string, values ...interface{}) error {
return s.Client.RPush(key, values...).Err()
}
// ListUnshift prepends to list
func (s *Storage) ListUnshift(key string, values ...interface{}) error {
return s.Client.LPush(key, values...).Err()
}
// ListShift removes first list element and returns it
func (s *Storage) ListShift(key string) (string, error) {
return s.Client.LPop(key).Result()
}
// ListBlockingPopAndPush Blocks until an item is available, pops it from a list and pushes it to another
// times out after 10 minutes
func (s *Storage) ListBlockingPopAndPush(from, to string, wait time.Duration) (string, error) {
return s.Client.BRPopLPush(from, to, wait).Result()
}
// ListRemove removes count of item from list
func (s *Storage) ListRemove(key string, count int64, value string) (int64, error) {
return s.Client.LRem(key, count, value).Result()
}
// Sets
// SetAdd adds member to a set at key
func (s *Storage) SetAdd(key string, member string) error {
return s.Client.SAdd(key, member).Err()
}
// GetSet gets set at key
func (s *Storage) SetGet(key string) ([]string, error) {
return s.Client.SMembers(key).Result()
}
func (s *Storage) SetHasMember(key string, member string) (bool, error) {
return s.Client.SIsMember(key, member).Result()
}
// SortedSetCount adds a member with score to sorted set
func (s *Storage) SortedSetCount(key, min, max string) (int64, error) {
return s.Client.ZCount(key, min, max).Result()
}
// SortedSetAdd adds a member with score to sorted set
func (s *Storage) SortedSetAdd(key string, member string, score float64) error {
return s.Client.ZAdd(key, redis.Z{Score: score, Member: member}).Err()
}
// SortedSetRemove adds a member with score to sorted set
func (s *Storage) SortedSetRemove(key string, member ...interface{}) (int64, error) {
return s.Client.ZRem(key, member...).Result()
}
// SortedSetUpdate only sets score if it exists
func (s *Storage) SortedSetUpdate(key string, member string, score float64) error {
return s.Client.ZAddXX(key, redis.Z{Score: score, Member: member}).Err()
}
// SortedSetGetByScore
func (s *Storage) SortedSetGetByScore(key string, min string, max string, limit int64) ([]string, error) {
r := redis.ZRangeBy{Min: min, Max: max, Count: limit}
return s.Client.ZRangeByScore(key, r).Result()
}
// SortedSetRemoveByScore
func (s *Storage) SortedSetRemoveByScore(key string, min string, max string) (int64, error) {
return s.Client.ZRemRangeByScore(key, min, max).Result()
}
// GetSortedSetLTH gets sorted set at key low to high
func (s *Storage) GetSortedSetLTH(key string, start int64, stop int64) ([]string, error) {
return s.Client.ZRange(key, start, stop).Result()
}
// GetSortedSetHTL gets sorted set at key high to low
func (s *Storage) GetSortedSetHTL(key string, start int64, stop int64) ([]string, error) {
return s.Client.ZRevRange(key, start, stop).Result()
}
// IncrementSortedSetMemberScore Increments the score of a sorted set member
func (s *Storage) IncrementSortedSetMemberScore(key string, member string, inc float64) error {
return s.Client.ZIncrBy(key, inc, member).Err()
}
// MoveSortedSetMemberUpdating moves a sorted set member between sets and updates score
func (s *Storage) MoveSortedSetMemberUpdating(fromKey string, toKey string, member string, newScore float64) error {
pipe := s.Client.TxPipeline()
pipe.ZAdd(toKey, redis.Z{Member: member, Score: newScore})
pipe.ZRem(fromKey, member)
_, err := pipe.Exec()
return err
}
// KeysPrefixCount returns the number of keys with a given prefix
func (s *Storage) KeysPrefixCount(prefix string) (int64, error) {
script := fmt.Sprintf("return #redis.pcall('keys', '%s*')", prefix)
result, err := s.Client.Eval(script, []string{}).Result()
count, _ := result.(int64)
return count, err
}
// SortedSetsCardSum returns the total number of elements in a list of sets
func (s *Storage) SortedSetsCardSum(prefix string) (int64, error) {
script := fmt.Sprintf(
`local sum = 0
local matches = redis.pcall('KEYS', '%s*')
for _,key in ipairs(matches) do
local val = redis.pcall('ZCARD', key)
sum = sum + tonumber(val)
end
return sum`, prefix)
result, err := s.Client.Eval(script, []string{}).Result()
count, _ := result.(int64)
return count, err
}
// GetScore gets core of member from set
func (s *Storage) GetScore(key string, member string) (float64, error) {
return s.Client.ZScore(key, member).Result()
}
// Events
// Publish pushes a message to a channel
func (s *Storage) Publish(channel string, message string) error {
return s.Client.Publish(channel, message).Err()
}
// Start connects to redis and starts listening for events
func (s *Storage) Start(subs ...string) {
s.subscribe(subs...)
}
func (s *Storage) subscribe(subs ...string) {
pubsub := s.Client.Subscribe(subs...)
defer pubsub.Close()
log.Debug("subscribed")
for {
msg, err := pubsub.ReceiveMessage()
if err != nil {
log.Error("failed to receive", err)
break
}
s.Receiver <- *msg
}
}
func createRedisClient(host, password string) *redis.Client {
for {
// Connect to redis
// Create connection to redis
redisClient := redis.NewClient(&redis.Options{
Addr: host,
Password: password, // no password set
DB: 0, // TODO use default DB, should not be hardcoded
})
_, err := redisClient.Ping().Result()
if err != nil {
log.Println("error connecting to redis", err)
time.Sleep(time.Second)
continue
}
log.Info(`Connected to redis`)
return redisClient
}
}