2024-02-18 10:42:21 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StringCmdable interface {
|
|
|
|
Append(ctx context.Context, key, value string) *IntCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
Decr(ctx context.Context, key string) *IntCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
DecrBy(ctx context.Context, key string, decrement int64) *IntCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
Get(ctx context.Context, key string) *StringCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
GetRange(ctx context.Context, key string, start, end int64) *StringCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
GetSet(ctx context.Context, key string, value interface{}) *StringCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
GetEx(ctx context.Context, key string, expiration time.Duration) *StringCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
GetDel(ctx context.Context, key string) *StringCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
Incr(ctx context.Context, key string) *IntCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
IncrBy(ctx context.Context, key string, value int64) *IntCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
IncrByFloat(ctx context.Context, key string, value float64) *FloatCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
LCS(ctx context.Context, q *LCSQuery) *LCSCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
MGet(ctx context.Context, keys ...string) *SliceCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
MSet(ctx context.Context, values ...interface{}) *StatusCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
MSetNX(ctx context.Context, values ...interface{}) *BoolCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
SetArgs(ctx context.Context, key string, value interface{}, a SetArgs) *StatusCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
SetEx(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
StrLen(ctx context.Context, key string) *IntCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) Append(ctx context.Context, key, value string) *IntCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewIntCmd(ctx, "append", key, value)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) Decr(ctx context.Context, key string) *IntCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewIntCmd(ctx, "decr", key)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) DecrBy(ctx context.Context, key string, decrement int64) *IntCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewIntCmd(ctx, "decrby", key, decrement)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get Redis `GET key` command. It returns redis.Nil error when key does not exist.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) Get(ctx context.Context, key string) *StringCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewStringCmd(ctx, "get", key)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) GetRange(ctx context.Context, key string, start, end int64) *StringCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewStringCmd(ctx, "getrange", key, start, end)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) GetSet(ctx context.Context, key string, value interface{}) *StringCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewStringCmd(ctx, "getset", key, value)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEx An expiration of zero removes the TTL associated with the key (i.e. GETEX key persist).
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// Requires Redis >= 6.2.0.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) GetEx(ctx context.Context, key string, expiration time.Duration) *StringCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args := make([]interface{}, 0, 4)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "getex", key)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
if expiration > 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
if usePrecise(expiration) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "px", formatMs(ctx, expiration))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
} else {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "ex", formatSec(ctx, expiration))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
} else if expiration == 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "persist")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := NewStringCmd(ctx, args...)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDel redis-server version >= 6.2.0.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) GetDel(ctx context.Context, key string) *StringCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewStringCmd(ctx, "getdel", key)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) Incr(ctx context.Context, key string) *IntCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewIntCmd(ctx, "incr", key)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) IncrBy(ctx context.Context, key string, value int64) *IntCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewIntCmd(ctx, "incrby", key, value)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) IncrByFloat(ctx context.Context, key string, value float64) *FloatCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewFloatCmd(ctx, "incrbyfloat", key, value)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) LCS(ctx context.Context, q *LCSQuery) *LCSCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewLCSCmd(ctx, q)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) MGet(ctx context.Context, keys ...string) *SliceCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args := make([]interface{}, 1+len(keys))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args[0] = "mget"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
for i, key := range keys {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args[1+i] = key
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewSliceCmd(ctx, args...)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MSet is like Set but accepts multiple values:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// - MSet("key1", "value1", "key2", "value2")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// - MSet([]string{"key1", "value1", "key2", "value2"})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// - MSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// - MSet(struct), For struct types, see HSet description.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) MSet(ctx context.Context, values ...interface{}) *StatusCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args := make([]interface{}, 1, 1+len(values))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args[0] = "mset"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = appendArgs(args, values)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewStatusCmd(ctx, args...)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MSetNX is like SetNX but accepts multiple values:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// - MSetNX("key1", "value1", "key2", "value2")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// - MSetNX([]string{"key1", "value1", "key2", "value2"})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// - MSetNX(map[string]interface{}{"key1": "value1", "key2": "value2"})
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// - MSetNX(struct), For struct types, see HSet description.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) MSetNX(ctx context.Context, values ...interface{}) *BoolCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args := make([]interface{}, 1, 1+len(values))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args[0] = "msetnx"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = appendArgs(args, values)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewBoolCmd(ctx, args...)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set Redis `SET key value [expiration]` command.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// Use expiration for `SETEx`-like behavior.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
//
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// Zero expiration means the key has no expiration time.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// KeepTTL is a Redis KEEPTTL option to keep existing TTL, it requires your redis-server version >= 6.0,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// otherwise you will receive an error: (error) ERR syntax error.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args := make([]interface{}, 3, 5)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args[0] = "set"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args[1] = key
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args[2] = value
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
if expiration > 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
if usePrecise(expiration) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "px", formatMs(ctx, expiration))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
} else {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "ex", formatSec(ctx, expiration))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
} else if expiration == KeepTTL {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "keepttl")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := NewStatusCmd(ctx, args...)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetArgs provides arguments for the SetArgs function.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
type SetArgs struct {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// Mode can be `NX` or `XX` or empty.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
Mode string
|
|
|
|
|
|
|
|
// Zero `TTL` or `Expiration` means that the key has no expiration time.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
TTL time.Duration
|
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
ExpireAt time.Time
|
|
|
|
|
|
|
|
// When Get is true, the command returns the old value stored at key, or nil when key did not exist.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
Get bool
|
|
|
|
|
|
|
|
// KeepTTL is a Redis KEEPTTL option to keep existing TTL, it requires your redis-server version >= 6.0,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// otherwise you will receive an error: (error) ERR syntax error.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
KeepTTL bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetArgs supports all the options that the SET command supports.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// It is the alternative to the Set function when you want
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// to have more control over the options.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) SetArgs(ctx context.Context, key string, value interface{}, a SetArgs) *StatusCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args := []interface{}{"set", key, value}
|
|
|
|
|
|
|
|
if a.KeepTTL {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "keepttl")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !a.ExpireAt.IsZero() {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "exat", a.ExpireAt.Unix())
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
if a.TTL > 0 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
if usePrecise(a.TTL) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "px", formatMs(ctx, a.TTL))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
} else {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "ex", formatSec(ctx, a.TTL))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.Mode != "" {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, a.Mode)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.Get {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
args = append(args, "get")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := NewStatusCmd(ctx, args...)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetEx Redis `SETEx key expiration value` command.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) SetEx(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewStatusCmd(ctx, "setex", key, formatSec(ctx, expiration), value)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetNX Redis `SET key value [expiration] NX` command.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
//
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// Zero expiration means the key has no expiration time.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// KeepTTL is a Redis KEEPTTL option to keep existing TTL, it requires your redis-server version >= 6.0,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// otherwise you will receive an error: (error) ERR syntax error.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
var cmd *BoolCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
switch expiration {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
case 0:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// Use old `SETNX` to support old Redis versions.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd = NewBoolCmd(ctx, "setnx", key, value)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
case KeepTTL:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "nx")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
default:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
if usePrecise(expiration) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd = NewBoolCmd(ctx, "set", key, value, "px", formatMs(ctx, expiration), "nx")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
} else {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "nx")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetXX Redis `SET key value [expiration] XX` command.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
//
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// Zero expiration means the key has no expiration time.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// KeepTTL is a Redis KEEPTTL option to keep existing TTL, it requires your redis-server version >= 6.0,
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// otherwise you will receive an error: (error) ERR syntax error.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func (c cmdable) SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
var cmd *BoolCmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
switch expiration {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
case 0:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd = NewBoolCmd(ctx, "set", key, value, "xx")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
case KeepTTL:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "xx")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
default:
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
if usePrecise(expiration) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd = NewBoolCmd(ctx, "set", key, value, "px", formatMs(ctx, expiration), "xx")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
} else {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "xx")
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewIntCmd(ctx, "setrange", key, offset, value)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c cmdable) StrLen(ctx context.Context, key string) *IntCmd {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
cmd := NewIntCmd(ctx, "strlen", key)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
_ = c(ctx, cmd)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return cmd
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|