forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/generic_commands.go

651 lines
11 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package redis
import (
"context"
"time"
)
type GenericCmdable interface {
Del(ctx context.Context, keys ...string) *IntCmd
2024-02-18 10:42:21 +00:00
Dump(ctx context.Context, key string) *StringCmd
2024-02-18 10:42:21 +00:00
Exists(ctx context.Context, keys ...string) *IntCmd
2024-02-18 10:42:21 +00:00
Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
2024-02-18 10:42:21 +00:00
ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
2024-02-18 10:42:21 +00:00
ExpireTime(ctx context.Context, key string) *DurationCmd
2024-02-18 10:42:21 +00:00
ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
2024-02-18 10:42:21 +00:00
ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
2024-02-18 10:42:21 +00:00
ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
2024-02-18 10:42:21 +00:00
ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
2024-02-18 10:42:21 +00:00
Keys(ctx context.Context, pattern string) *StringSliceCmd
2024-02-18 10:42:21 +00:00
Migrate(ctx context.Context, host, port, key string, db int, timeout time.Duration) *StatusCmd
2024-02-18 10:42:21 +00:00
Move(ctx context.Context, key string, db int) *BoolCmd
2024-02-18 10:42:21 +00:00
ObjectRefCount(ctx context.Context, key string) *IntCmd
2024-02-18 10:42:21 +00:00
ObjectEncoding(ctx context.Context, key string) *StringCmd
2024-02-18 10:42:21 +00:00
ObjectIdleTime(ctx context.Context, key string) *DurationCmd
2024-02-18 10:42:21 +00:00
Persist(ctx context.Context, key string) *BoolCmd
2024-02-18 10:42:21 +00:00
PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
2024-02-18 10:42:21 +00:00
PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
2024-02-18 10:42:21 +00:00
PExpireTime(ctx context.Context, key string) *DurationCmd
2024-02-18 10:42:21 +00:00
PTTL(ctx context.Context, key string) *DurationCmd
2024-02-18 10:42:21 +00:00
RandomKey(ctx context.Context) *StringCmd
2024-02-18 10:42:21 +00:00
Rename(ctx context.Context, key, newkey string) *StatusCmd
2024-02-18 10:42:21 +00:00
RenameNX(ctx context.Context, key, newkey string) *BoolCmd
2024-02-18 10:42:21 +00:00
Restore(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
2024-02-18 10:42:21 +00:00
RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
2024-02-18 10:42:21 +00:00
Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd
2024-02-18 10:42:21 +00:00
SortRO(ctx context.Context, key string, sort *Sort) *StringSliceCmd
2024-02-18 10:42:21 +00:00
SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd
2024-02-18 10:42:21 +00:00
SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd
2024-02-18 10:42:21 +00:00
Touch(ctx context.Context, keys ...string) *IntCmd
2024-02-18 10:42:21 +00:00
TTL(ctx context.Context, key string) *DurationCmd
2024-02-18 10:42:21 +00:00
Type(ctx context.Context, key string) *StatusCmd
2024-02-18 10:42:21 +00:00
Copy(ctx context.Context, sourceKey string, destKey string, db int, replace bool) *IntCmd
Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd
2024-02-18 10:42:21 +00:00
ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *ScanCmd
}
func (c cmdable) Del(ctx context.Context, keys ...string) *IntCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 1+len(keys))
2024-02-18 10:42:21 +00:00
args[0] = "del"
2024-02-18 10:42:21 +00:00
for i, key := range keys {
2024-02-18 10:42:21 +00:00
args[1+i] = key
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Unlink(ctx context.Context, keys ...string) *IntCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 1+len(keys))
2024-02-18 10:42:21 +00:00
args[0] = "unlink"
2024-02-18 10:42:21 +00:00
for i, key := range keys {
2024-02-18 10:42:21 +00:00
args[1+i] = key
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Dump(ctx context.Context, key string) *StringCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStringCmd(ctx, "dump", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Exists(ctx context.Context, keys ...string) *IntCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 1+len(keys))
2024-02-18 10:42:21 +00:00
args[0] = "exists"
2024-02-18 10:42:21 +00:00
for i, key := range keys {
2024-02-18 10:42:21 +00:00
args[1+i] = key
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
2024-02-18 10:42:21 +00:00
return c.expire(ctx, key, expiration, "")
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
2024-02-18 10:42:21 +00:00
return c.expire(ctx, key, expiration, "NX")
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
2024-02-18 10:42:21 +00:00
return c.expire(ctx, key, expiration, "XX")
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
2024-02-18 10:42:21 +00:00
return c.expire(ctx, key, expiration, "GT")
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
2024-02-18 10:42:21 +00:00
return c.expire(ctx, key, expiration, "LT")
2024-02-18 10:42:21 +00:00
}
func (c cmdable) expire(
2024-02-18 10:42:21 +00:00
ctx context.Context, key string, expiration time.Duration, mode string,
2024-02-18 10:42:21 +00:00
) *BoolCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 3, 4)
2024-02-18 10:42:21 +00:00
args[0] = "expire"
2024-02-18 10:42:21 +00:00
args[1] = key
2024-02-18 10:42:21 +00:00
args[2] = formatSec(ctx, expiration)
2024-02-18 10:42:21 +00:00
if mode != "" {
2024-02-18 10:42:21 +00:00
args = append(args, mode)
2024-02-18 10:42:21 +00:00
}
cmd := NewBoolCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
2024-02-18 10:42:21 +00:00
cmd := NewBoolCmd(ctx, "expireat", key, tm.Unix())
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ExpireTime(ctx context.Context, key string) *DurationCmd {
2024-02-18 10:42:21 +00:00
cmd := NewDurationCmd(ctx, time.Second, "expiretime", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Keys(ctx context.Context, pattern string) *StringSliceCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStringSliceCmd(ctx, "keys", pattern)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Migrate(ctx context.Context, host, port, key string, db int, timeout time.Duration) *StatusCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStatusCmd(
2024-02-18 10:42:21 +00:00
ctx,
2024-02-18 10:42:21 +00:00
"migrate",
2024-02-18 10:42:21 +00:00
host,
2024-02-18 10:42:21 +00:00
port,
2024-02-18 10:42:21 +00:00
key,
2024-02-18 10:42:21 +00:00
db,
2024-02-18 10:42:21 +00:00
formatMs(ctx, timeout),
)
2024-02-18 10:42:21 +00:00
cmd.setReadTimeout(timeout)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Move(ctx context.Context, key string, db int) *BoolCmd {
2024-02-18 10:42:21 +00:00
cmd := NewBoolCmd(ctx, "move", key, db)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ObjectRefCount(ctx context.Context, key string) *IntCmd {
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, "object", "refcount", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ObjectEncoding(ctx context.Context, key string) *StringCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStringCmd(ctx, "object", "encoding", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ObjectIdleTime(ctx context.Context, key string) *DurationCmd {
2024-02-18 10:42:21 +00:00
cmd := NewDurationCmd(ctx, time.Second, "object", "idletime", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Persist(ctx context.Context, key string) *BoolCmd {
2024-02-18 10:42:21 +00:00
cmd := NewBoolCmd(ctx, "persist", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
2024-02-18 10:42:21 +00:00
cmd := NewBoolCmd(ctx, "pexpire", key, formatMs(ctx, expiration))
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
2024-02-18 10:42:21 +00:00
cmd := NewBoolCmd(
2024-02-18 10:42:21 +00:00
ctx,
2024-02-18 10:42:21 +00:00
"pexpireat",
2024-02-18 10:42:21 +00:00
key,
2024-02-18 10:42:21 +00:00
tm.UnixNano()/int64(time.Millisecond),
)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) PExpireTime(ctx context.Context, key string) *DurationCmd {
2024-02-18 10:42:21 +00:00
cmd := NewDurationCmd(ctx, time.Millisecond, "pexpiretime", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) PTTL(ctx context.Context, key string) *DurationCmd {
2024-02-18 10:42:21 +00:00
cmd := NewDurationCmd(ctx, time.Millisecond, "pttl", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) RandomKey(ctx context.Context) *StringCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStringCmd(ctx, "randomkey")
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Rename(ctx context.Context, key, newkey string) *StatusCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStatusCmd(ctx, "rename", key, newkey)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) RenameNX(ctx context.Context, key, newkey string) *BoolCmd {
2024-02-18 10:42:21 +00:00
cmd := NewBoolCmd(ctx, "renamenx", key, newkey)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Restore(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStatusCmd(
2024-02-18 10:42:21 +00:00
ctx,
2024-02-18 10:42:21 +00:00
"restore",
2024-02-18 10:42:21 +00:00
key,
2024-02-18 10:42:21 +00:00
formatMs(ctx, ttl),
2024-02-18 10:42:21 +00:00
value,
)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStatusCmd(
2024-02-18 10:42:21 +00:00
ctx,
2024-02-18 10:42:21 +00:00
"restore",
2024-02-18 10:42:21 +00:00
key,
2024-02-18 10:42:21 +00:00
formatMs(ctx, ttl),
2024-02-18 10:42:21 +00:00
value,
2024-02-18 10:42:21 +00:00
"replace",
)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
type Sort struct {
By string
2024-02-18 10:42:21 +00:00
Offset, Count int64
Get []string
Order string
Alpha bool
2024-02-18 10:42:21 +00:00
}
func (sort *Sort) args(command, key string) []interface{} {
2024-02-18 10:42:21 +00:00
args := []interface{}{command, key}
if sort.By != "" {
2024-02-18 10:42:21 +00:00
args = append(args, "by", sort.By)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sort.Offset != 0 || sort.Count != 0 {
2024-02-18 10:42:21 +00:00
args = append(args, "limit", sort.Offset, sort.Count)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for _, get := range sort.Get {
2024-02-18 10:42:21 +00:00
args = append(args, "get", get)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sort.Order != "" {
2024-02-18 10:42:21 +00:00
args = append(args, sort.Order)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sort.Alpha {
2024-02-18 10:42:21 +00:00
args = append(args, "alpha")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return args
2024-02-18 10:42:21 +00:00
}
func (c cmdable) SortRO(ctx context.Context, key string, sort *Sort) *StringSliceCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStringSliceCmd(ctx, sort.args("sort_ro", key)...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStringSliceCmd(ctx, sort.args("sort", key)...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd {
2024-02-18 10:42:21 +00:00
args := sort.args("sort", key)
2024-02-18 10:42:21 +00:00
if store != "" {
2024-02-18 10:42:21 +00:00
args = append(args, "store", store)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd {
2024-02-18 10:42:21 +00:00
cmd := NewSliceCmd(ctx, sort.args("sort", key)...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Touch(ctx context.Context, keys ...string) *IntCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, len(keys)+1)
2024-02-18 10:42:21 +00:00
args[0] = "touch"
2024-02-18 10:42:21 +00:00
for i, key := range keys {
2024-02-18 10:42:21 +00:00
args[i+1] = key
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) TTL(ctx context.Context, key string) *DurationCmd {
2024-02-18 10:42:21 +00:00
cmd := NewDurationCmd(ctx, time.Second, "ttl", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Type(ctx context.Context, key string) *StatusCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStatusCmd(ctx, "type", key)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) Copy(ctx context.Context, sourceKey, destKey string, db int, replace bool) *IntCmd {
2024-02-18 10:42:21 +00:00
args := []interface{}{"copy", sourceKey, destKey, "DB", db}
2024-02-18 10:42:21 +00:00
if replace {
2024-02-18 10:42:21 +00:00
args = append(args, "REPLACE")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
//------------------------------------------------------------------------------
func (c cmdable) Scan(ctx context.Context, cursor uint64, match string, count int64) *ScanCmd {
2024-02-18 10:42:21 +00:00
args := []interface{}{"scan", cursor}
2024-02-18 10:42:21 +00:00
if match != "" {
2024-02-18 10:42:21 +00:00
args = append(args, "match", match)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if count > 0 {
2024-02-18 10:42:21 +00:00
args = append(args, "count", count)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewScanCmd(ctx, c, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) ScanType(ctx context.Context, cursor uint64, match string, count int64, keyType string) *ScanCmd {
2024-02-18 10:42:21 +00:00
args := []interface{}{"scan", cursor}
2024-02-18 10:42:21 +00:00
if match != "" {
2024-02-18 10:42:21 +00:00
args = append(args, "match", match)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if count > 0 {
2024-02-18 10:42:21 +00:00
args = append(args, "count", count)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if keyType != "" {
2024-02-18 10:42:21 +00:00
args = append(args, "type", keyType)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewScanCmd(ctx, c, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}