-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_group_list.go
263 lines (241 loc) · 11.3 KB
/
redis_group_list.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/joy12825/gf.
package redis
import (
"context"
"github.com/joy12825/gf/container/gvar"
"github.com/joy12825/gf/database/gredis"
"github.com/joy12825/gf/util/gconv"
)
// GroupList is the redis group list object.
type GroupList struct {
redis *Redis
}
// GroupList creates and returns a redis group object for list operations.
func (r *Redis) GroupList() gredis.IGroupList {
return GroupList{
redis: r,
}
}
// LPush inserts all the specified values at the head of the list stored at key
// Insert all the specified values at the head of the list stored at key.
// If key does not exist, it is created as empty list before performing the push operations.
// When key holds a value that is not a list, an error is returned.
//
// It returns the length of the list after the push operations.
//
// https://redis.io/commands/lpush/
func (r GroupList) LPush(ctx context.Context, key string, values ...interface{}) (int64, error) {
v, err := r.redis.Do(ctx, "LPush", append([]interface{}{key}, values...)...)
return v.Int64(), err
}
// LPushX insert value at the head of the list stored at key, only if key exists and holds a list.
// Inserts specified values at the head of the list stored at key, only if key already exists and holds a list.
// In contrary to LPush, no operation will be performed when key does not yet exist.
// Return Integer reply: the length of the list after the push operation.
//
// It returns the length of the list after the push operations.
//
// https://redis.io/commands/lpushx
func (r GroupList) LPushX(ctx context.Context, key string, element interface{}, elements ...interface{}) (int64, error) {
v, err := r.redis.Do(ctx, "LPushX", append([]interface{}{key, element}, elements...)...)
return v.Int64(), err
}
// RPush inserts all the specified values at the tail of the list stored at key.
// Insert all the specified values at the tail of the list stored at key.
// If key does not exist, it is created as empty list before performing the push operation.
//
// When key holds a value that is not a list, an error is returned.
// It is possible to push multiple elements using a single command call just specifying multiple
// arguments at the end of the command. Elements are inserted one after the other to the tail of the
// list, from the leftmost element to the rightmost element.
// So for instance the command RPush mylist a b c will result into a list containing a as first element,
// b as second element and c as third element.
//
// It returns the length of the list after the push operation.
//
// https://redis.io/commands/rpush
func (r GroupList) RPush(ctx context.Context, key string, values ...interface{}) (int64, error) {
v, err := r.redis.Do(ctx, "RPush", append([]interface{}{key}, values...)...)
return v.Int64(), err
}
// RPushX inserts value at the tail of the list stored at key, only if key exists and holds a list.
// Inserts specified values at the tail of the list stored at key, only if key already exists and
// holds a list.
//
// In contrary to RPush, no operation will be performed when key does not yet exist.
//
// It returns the length of the list after the push operation.
//
// https://redis.io/commands/rpushx
func (r GroupList) RPushX(ctx context.Context, key string, value interface{}) (int64, error) {
v, err := r.redis.Do(ctx, "RPushX", key, value)
return v.Int64(), err
}
// LPop remove and returns the first element of the list stored at key.
// Removes and returns the first elements of the list stored at key.
//
// Starting with Redis version 6.2.0: Added the count argument.
//
// By default, the command pops a single element from the beginning of the list.
// When provided with the optional count argument, the reply will consist of up to count elements,
// depending on the list's length.
//
// Return When called without the count argument:
// Bulk string reply: the value of the first element, or nil when key does not exist.
//
// When called with the count argument:
// Array reply: list of popped elements, or nil when key does not exist.
//
// https://redis.io/commands/lpop
func (r GroupList) LPop(ctx context.Context, key string, count ...int) (*gvar.Var, error) {
if len(count) > 0 {
return r.redis.Do(ctx, "LPop", key, count[0])
}
return r.redis.Do(ctx, "LPop", key)
}
// RPop remove and returns the last element of the list stored at key.
// Removes and returns the last elements of the list stored at key.
//
// Starting with Redis version 6.2.0: Added the count argument.
//
// By default, the command pops a single element from the end of the list.
// When provided with the optional count argument, the reply will consist of up to count elements,
// depending on the list's length.
//
// It returns:
// - When called without the count argument:
// the value of the last element, or nil when key does not exist.
// - When called with the count argument:
// list of popped elements, or nil when key does not exist.
//
// https://redis.io/commands/rpop
func (r GroupList) RPop(ctx context.Context, key string, count ...int) (*gvar.Var, error) {
if len(count) > 0 {
return r.redis.Do(ctx, "RPop", key, count[0])
}
return r.redis.Do(ctx, "RPop", key)
}
// LRem removes the first count occurrences of elements equal to value from the list stored at key.
//
// It returns the number of removed elements.
//
// https://redis.io/commands/lrem/
func (r GroupList) LRem(ctx context.Context, key string, count int64, value interface{}) (int64, error) {
v, err := r.redis.Do(ctx, "LRem", key, count, value)
return v.Int64(), err
}
// LLen returns the length of the list stored at key.
// Returns the length of the list stored at key.
// If key does not exist, it is interpreted as an empty list and 0 is returned.
// An error is returned when the value stored at key is not a list.
//
// https://redis.io/commands/llen
func (r GroupList) LLen(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "LLen", key)
return v.Int64(), err
}
// LIndex return the element at index in the list stored at key.
// Returns the element at index in the list stored at key.
// The index is zero-based, so 0 means the first element, 1 the second element and so on.
// Negative indices can be used to designate elements starting at the tail of the list.
// Here, -1 means the last element, -2 means the penultimate and so forth.
// When the value at key is not a list, an error is returned.
//
// It returns the requested element, or nil when index is out of range.
//
// https://redis.io/commands/lindex
func (r GroupList) LIndex(ctx context.Context, key string, index int64) (*gvar.Var, error) {
return r.redis.Do(ctx, "LIndex", key, index)
}
// LInsert inserts element in the list stored at key either before or after the reference value pivot.
// When key does not exist, it is considered an empty list and no operation is performed.
// An error is returned when key exists but does not hold a list value.
//
// It returns the length of the list after the insert operation, or -1 when the value pivot was not found.
//
// https://redis.io/commands/linsert/
func (r GroupList) LInsert(ctx context.Context, key string, op gredis.LInsertOp, pivot, value interface{}) (int64, error) {
v, err := r.redis.Do(ctx, "LInsert", key, string(op), pivot, value)
return v.Int64(), err
}
// LSet sets the list element at index to element.
// For more information on the index argument, see LIndex.
// An error is returned for out of range indexes.
//
// https://redis.io/commands/lset/
func (r GroupList) LSet(ctx context.Context, key string, index int64, value interface{}) (*gvar.Var, error) {
return r.redis.Do(ctx, "LSet", key, index, value)
}
// LRange returns the specified elements of the list stored at key.
// The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the
// head of the list), 1 being the next element and so on.
//
// These offsets can also be negative numbers indicating offsets starting at the end of the list.
// For example, -1 is the last element of the list, -2 the penultimate, and so on.
//
// https://redis.io/commands/lrange/
func (r GroupList) LRange(ctx context.Context, key string, start, stop int64) (gvar.Vars, error) {
v, err := r.redis.Do(ctx, "LRange", key, start, stop)
return v.Vars(), err
}
// LTrim trims an existing list so that it will contain only the specified range of elements
// specified. Both start and stop are zero-based indexes, where 0 is the first element of the list
// (the head), 1 the next element and so on.
//
// https://redis.io/commands/ltrim/
func (r GroupList) LTrim(ctx context.Context, key string, start, stop int64) error {
_, err := r.redis.Do(ctx, "LTrim", key, start, stop)
return err
}
// BLPop is a blocking list pop primitive.
// It is the blocking version of LPop because it blocks the connection when there are no elements to
// pop from any of the given lists.
// An element is popped from the head of the first list that is non-empty, with the given keys being
// checked in the order that they are given.
//
// The timeout argument is interpreted as a double value specifying the maximum number of seconds to
// block. A timeout of zero can be used to block indefinitely.
//
// https://redis.io/commands/blpop/
func (r GroupList) BLPop(ctx context.Context, timeout int64, keys ...string) (gvar.Vars, error) {
v, err := r.redis.Do(ctx, "BLPop", append(gconv.Interfaces(keys), timeout)...)
return v.Vars(), err
}
// BRPop is a blocking list pop primitive.
// It is the blocking version of RPop because it blocks the connection when there are no elements to
// pop from any of the given lists. An element is popped from the tail of the first list that is
// non-empty, with the given keys being checked in the order that they are given.
//
// The timeout argument is interpreted as a double value specifying the maximum number of seconds to
// block. A timeout of zero can be used to block indefinitely.
//
// https://redis.io/commands/brpop/
func (r GroupList) BRPop(ctx context.Context, timeout int64, keys ...string) (gvar.Vars, error) {
v, err := r.redis.Do(ctx, "BRPop", append(gconv.Interfaces(keys), timeout)...)
return v.Vars(), err
}
// RPopLPush remove the last element in list source, appends it to the front of list destination and
// returns it.
//
// https://redis.io/commands/rpoplpush/
func (r GroupList) RPopLPush(ctx context.Context, source, destination string) (*gvar.Var, error) {
return r.redis.Do(ctx, "RPopLPush", source, destination)
}
// BRPopLPush is the blocking variant of RPopLPush.
// When source contains elements, this command behaves exactly like RPopLPush. When used inside a
// MULTI/EXEC block,
// this command behaves exactly like RPopLPush. When source is empty, Redis will block the connection
// until another // client pushes to it or until timeout is reached.
// A timeout of zero can be used to block indefinitely.
//
// It returns the element being popped from source and pushed to destination.
// If timeout is reached, a Null reply is returned.
//
// https://redis.io/commands/brpoplpush/
func (r GroupList) BRPopLPush(ctx context.Context, source, destination string, timeout int64) (*gvar.Var, error) {
return r.redis.Do(ctx, "BRPopLPush", source, destination, timeout)
}