-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_group_generic.go
341 lines (314 loc) · 12.6 KB
/
redis_group_generic.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// 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"
"time"
"github.com/joy12825/gf/container/gvar"
"github.com/joy12825/gf/database/gredis"
"github.com/joy12825/gf/os/gtime"
"github.com/joy12825/gf/util/gconv"
)
// GroupGeneric provides generic functions of redis.
type GroupGeneric struct {
redis *Redis
}
// GroupGeneric creates and returns GroupGeneric.
func (r *Redis) GroupGeneric() gredis.IGroupGeneric {
return GroupGeneric{
redis: r,
}
}
// Copy copies the value stored at the source key to the destination key.
//
// By default, the destination key is created in the logical database used by the connection.
// The DB option allows specifying an alternative logical database index for the destination key.
//
// The command returns an error when the destination key already exists.
//
// It returns:
// - 1 if source was copied.
// - 0 if source was not copied.
//
// https://redis.io/commands/copy/
func (r GroupGeneric) Copy(ctx context.Context, source, destination string, option ...gredis.CopyOption) (int64, error) {
var usedOption interface{}
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "Copy", mustMergeOptionToArgs(
[]interface{}{source, destination}, usedOption,
)...)
return v.Int64(), err
}
// Exists returns if key exists.
// The user should be aware that if the same existing key is mentioned in the arguments multiple times,
// it will be counted multiple times.
//
// It returns the number of keys that exist from those specified as arguments.
//
// https://redis.io/commands/exists/
func (r GroupGeneric) Exists(ctx context.Context, keys ...string) (int64, error) {
v, err := r.redis.Do(ctx, "Exists", gconv.Interfaces(keys)...)
return v.Int64(), err
}
// Type returns the string representation of the type of the value stored at key.
// The different types that can be returned are: string, list, set, zset, hash and stream.
//
// It returns type of key, or none when key does not exist.
//
// https://redis.io/commands/type/
func (r GroupGeneric) Type(ctx context.Context, key string) (string, error) {
v, err := r.redis.Do(ctx, "Type", key)
return v.String(), err
}
// Unlink is very similar to DEL: it removes the specified keys. Just like DEL a key is ignored if it does not exist.
// However, the command performs the actual memory reclaiming in a different thread, so it is not blocking, while DEL is.
// This is where the command name comes from: the command just unlinks the keys from the keyspace.
// The actual removal will happen later asynchronously.
//
// It returns the number of keys that were unlinked.
//
// https://redis.io/commands/unlink/
func (r GroupGeneric) Unlink(ctx context.Context, keys ...string) (int64, error) {
v, err := r.redis.Do(ctx, "Unlink", gconv.Interfaces(keys)...)
return v.Int64(), err
}
// Rename renames key to newKey. It returns an error when key does not exist.
// If newKey already exists it is overwritten, when this happens RENAME executes an implicit DEL operation,
// so if the deleted key contains a very big value it may cause high latency even if RENAME itself is usually a constant-time operation.
//
// In Cluster mode, both key and newKey must be in the same hash slot,
// meaning that in practice only keys that have the same hashtag can be reliably renamed in cluster.
//
// https://redis.io/commands/rename/
func (r GroupGeneric) Rename(ctx context.Context, key, newKey string) error {
_, err := r.redis.Do(ctx, "Rename", key, newKey)
return err
}
// RenameNX renames key to newKey if newKey does not yet exist.
// It returns an error when key does not exist.
// In Cluster mode, both key and newKey must be in the same hash slot,
// meaning that in practice only keys that have the same hashtag can be reliably renamed in cluster.
//
// It returns:
// - 1 if key was renamed to newKey.
// - 0 if newKey already exists.
//
// https://redis.io/commands/renamenx/
func (r GroupGeneric) RenameNX(ctx context.Context, key, newKey string) (int64, error) {
v, err := r.redis.Do(ctx, "RenameNX", key, newKey)
return v.Int64(), err
}
// Move moves key from the currently selected database (see SELECT) to the specified destination database.
// When key already exists in the destination database, or it does not exist in the source database,
// it does nothing.
// It is possible to use MOVE as a locking primitive because of this.
//
// It returns:
// - 1 if key was moved.
// - 0 if key was not moved.
//
// https://redis.io/commands/move/
func (r GroupGeneric) Move(ctx context.Context, key string, db int) (int64, error) {
v, err := r.redis.Do(ctx, "Move", key, db)
return v.Int64(), err
}
// Del removes the specified keys.
// a key is ignored if it does not exist.
//
// It returns the number of keys that were removed.
//
// https://redis.io/commands/del/
func (r GroupGeneric) Del(ctx context.Context, keys ...string) (int64, error) {
v, err := r.redis.Do(ctx, "Del", gconv.Interfaces(keys)...)
return v.Int64(), err
}
// RandomKey return a random key from the currently selected database.
//
// It returns the random key, or nil when the database is empty.
//
// https://redis.io/commands/randomkey/
func (r GroupGeneric) RandomKey(ctx context.Context) (string, error) {
v, err := r.redis.Do(ctx, "RandomKey")
return v.String(), err
}
// DBSize return the number of keys in the currently-selected database.
//
// https://redis.io/commands/dbsize/
func (r GroupGeneric) DBSize(ctx context.Context) (int64, error) {
v, err := r.redis.Do(ctx, "DBSize")
return v.Int64(), err
}
// Keys return all keys matching pattern.
//
// While the time complexity for this operation is O(N), the constant times are fairly low.
// For example, Redis running on an entry level laptop can scan a 1 million key database in 40 milliseconds.
//
// https://redis.io/commands/keys/
func (r GroupGeneric) Keys(ctx context.Context, pattern string) ([]string, error) {
v, err := r.redis.Do(ctx, "Keys", pattern)
return v.Strings(), err
}
// FlushDB delete all the keys of the currently selected DB. This command never fails.
//
// https://redis.io/commands/flushdb/
func (r GroupGeneric) FlushDB(ctx context.Context, option ...gredis.FlushOp) error {
_, err := r.redis.Do(ctx, "FlushDB", gconv.Interfaces(option)...)
return err
}
// FlushAll delete all the keys of all the existing databases, not just the currently selected one.
// This command never fails.
// By default, FlushAll will synchronously flush all the databases.
//
// It is possible to use one of the following modifiers to dictate the flushing mode explicitly:
// ASYNC: flushes the databases asynchronously
// SYNC: flushes the databases synchronously
//
// Note: an asynchronous FlushAll command only deletes keys that were present at the time the command was invoked.
// Keys created during an asynchronous flush will be unaffected.
//
// https://redis.io/commands/flushall/
func (r GroupGeneric) FlushAll(ctx context.Context, option ...gredis.FlushOp) error {
_, err := r.redis.Do(ctx, "FlushAll", gconv.Interfaces(option)...)
return err
}
// Expire sets a timeout on key.
// After the timeout has expired, the key will automatically be deleted.
//
// It returns:
// - 1 if the timeout was set.
// - 0 if the timeout was not set. e.g. key doesn't exist, or operation skipped due to the provided arguments.
//
// https://redis.io/commands/expire/
func (r GroupGeneric) Expire(ctx context.Context, key string, seconds int64, option ...gredis.ExpireOption) (int64, error) {
var usedOption interface{}
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "Expire", mustMergeOptionToArgs(
[]interface{}{key, seconds}, usedOption,
)...)
return v.Int64(), err
}
// ExpireAt has the same effect and semantic as EXPIRE, but instead of specifying the number of
// seconds representing the TTL (time to live), it takes an absolute Unix timestamp (seconds since
// January 1, 1970).
// A timestamp in the past will delete the key immediately.
//
// It returns:
// - 1 if the timeout was set.
// - 0 if the timeout was not set. e.g. key doesn't exist, or operation skipped due to the provided arguments.
//
// https://redis.io/commands/expireat/
func (r GroupGeneric) ExpireAt(ctx context.Context, key string, time time.Time, option ...gredis.ExpireOption) (int64, error) {
var usedOption interface{}
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "ExpireAt", mustMergeOptionToArgs(
[]interface{}{key, gtime.New(time).Timestamp()}, usedOption,
)...)
return v.Int64(), err
}
// ExpireTime returns the absolute time at which the given key will expire.
//
// It returns:
// - -1 if the key exists but has no associated expiration time.
// - -2 if the key does not exist.
//
// https://redis.io/commands/expiretime/
func (r GroupGeneric) ExpireTime(ctx context.Context, key string) (*gvar.Var, error) {
return r.redis.Do(ctx, "ExpireTime", key)
}
// TTL returns the remaining time to live of a key that has a timeout.
// This introspection capability allows a Redis client to check how many seconds a given key
// will continue to be part of the dataset.
// In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has
// no associated expire.
//
// Starting with Redis 2.8 the return value in case of error changed:
//
// The command returns -2 if the key does not exist.
// The command returns -1 if the key exists but has no associated expire.
// See also the PTTL command that returns the same information with milliseconds resolution
// (Only available in Redis 2.6 or greater).
//
// It returns TTL in seconds, or a negative value in order to signal an error (see the description above).
//
// https://redis.io/commands/ttl/
func (r GroupGeneric) TTL(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "TTL", key)
return v.Int64(), err
}
// Persist removes the existing timeout on key, turning the key from volatile (a key with an expire set)
// to persistent (a key that will never expire as no timeout is associated).
//
// It returns:
// - 1 if the timeout was removed.
// - 0 if key does not exist or does not have an associated timeout.
//
// https://redis.io/commands/persist/
func (r GroupGeneric) Persist(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "Persist", key)
return v.Int64(), err
}
// PExpire works exactly like EXPIRE but the time to live of the key is specified in milliseconds
// instead of seconds.
//
// It returns:
// - 1 if the timeout was set.
// - 0 if the timeout was not set. e.g. key doesn't exist, or operation skipped due to the provided arguments.
//
// https://redis.io/commands/pexpire/
func (r GroupGeneric) PExpire(ctx context.Context, key string, milliseconds int64, option ...gredis.ExpireOption) (int64, error) {
var usedOption interface{}
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "PExpire", mustMergeOptionToArgs(
[]interface{}{key, milliseconds}, usedOption,
)...)
return v.Int64(), err
}
// PExpireAt has the same effect and semantic as ExpireAt, but the Unix time at which the key will
// expire is specified in milliseconds instead of seconds.
//
// https://redis.io/commands/pexpireat/
func (r GroupGeneric) PExpireAt(ctx context.Context, key string, time time.Time, option ...gredis.ExpireOption) (int64, error) {
var usedOption interface{}
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "PExpireAt", mustMergeOptionToArgs(
[]interface{}{key, gtime.New(time).TimestampMilli()}, usedOption,
)...)
return v.Int64(), err
}
// PExpireTime returns the expiration time of given `key`.
//
// It returns:
// - -1 if the key exists but has no associated expiration time.
// - -2 if the key does not exist.
//
// https://redis.io/commands/pexpiretime/
func (r GroupGeneric) PExpireTime(ctx context.Context, key string) (*gvar.Var, error) {
return r.redis.Do(ctx, "PExpireTime", key)
}
// PTTL like TTL this command returns the remaining time to live of a key that has an expired set,
// with the sole difference that TTL returns the amount of remaining time in seconds while PTTL
// returns it in milliseconds.
//
// In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has
// no associated expire.
//
// It returns TTL in milliseconds, or a negative value in order to signal an error (see the description above).
//
// https://redis.io/commands/pttl/
func (r GroupGeneric) PTTL(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "PTTL", key)
return v.Int64(), err
}