mirror of
https://github.com/redis/go-redis.git
synced 2025-07-29 17:41:15 +03:00
feature: add support for set keepttl (#1499)
* feature: add support for set keepttl
This commit is contained in:
26
commands.go
26
commands.go
@ -9,6 +9,10 @@ import (
|
||||
"github.com/go-redis/redis/v8/internal"
|
||||
)
|
||||
|
||||
// KeepTTL used when set with keepttl option
|
||||
// Example: Set(ctx, key, value, redis.KeepTTL).
|
||||
const KeepTTL = -1
|
||||
|
||||
func usePrecise(dur time.Duration) bool {
|
||||
return dur < time.Second || dur%time.Second != 0
|
||||
}
|
||||
@ -756,6 +760,7 @@ func (c cmdable) MSetNX(ctx context.Context, values ...interface{}) *BoolCmd {
|
||||
//
|
||||
// Use expiration for `SETEX`-like behavior.
|
||||
// Zero expiration means the key has no expiration time.
|
||||
// KeepTTL(-1) expiration means command set adds keepttl option.
|
||||
func (c cmdable) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
|
||||
args := make([]interface{}, 3, 5)
|
||||
args[0] = "set"
|
||||
@ -767,7 +772,10 @@ func (c cmdable) Set(ctx context.Context, key string, value interface{}, expirat
|
||||
} else {
|
||||
args = append(args, "ex", formatSec(ctx, expiration))
|
||||
}
|
||||
} else if expiration == KeepTTL {
|
||||
args = append(args, "keepttl")
|
||||
}
|
||||
|
||||
cmd := NewStatusCmd(ctx, args...)
|
||||
_ = c(ctx, cmd)
|
||||
return cmd
|
||||
@ -776,18 +784,23 @@ func (c cmdable) Set(ctx context.Context, key string, value interface{}, expirat
|
||||
// Redis `SET key value [expiration] NX` command.
|
||||
//
|
||||
// Zero expiration means the key has no expiration time.
|
||||
// KeepTTL(-1) expiration means set command adds keepttl option.
|
||||
func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd {
|
||||
var cmd *BoolCmd
|
||||
if expiration == 0 {
|
||||
switch {
|
||||
case expiration == 0:
|
||||
// Use old `SETNX` to support old Redis versions.
|
||||
cmd = NewBoolCmd(ctx, "setnx", key, value)
|
||||
} else {
|
||||
case expiration == KeepTTL:
|
||||
cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "nx")
|
||||
default:
|
||||
if usePrecise(expiration) {
|
||||
cmd = NewBoolCmd(ctx, "set", key, value, "px", formatMs(ctx, expiration), "nx")
|
||||
} else {
|
||||
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "nx")
|
||||
}
|
||||
}
|
||||
|
||||
_ = c(ctx, cmd)
|
||||
return cmd
|
||||
}
|
||||
@ -795,17 +808,22 @@ func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expir
|
||||
// Redis `SET key value [expiration] XX` command.
|
||||
//
|
||||
// Zero expiration means the key has no expiration time.
|
||||
// KeepTTL(-1) expiration means set command adds keepttl option.
|
||||
func (c cmdable) SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd {
|
||||
var cmd *BoolCmd
|
||||
if expiration == 0 {
|
||||
switch {
|
||||
case expiration == 0:
|
||||
cmd = NewBoolCmd(ctx, "set", key, value, "xx")
|
||||
} else {
|
||||
case expiration == KeepTTL:
|
||||
cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "xx")
|
||||
default:
|
||||
if usePrecise(expiration) {
|
||||
cmd = NewBoolCmd(ctx, "set", key, value, "px", formatMs(ctx, expiration), "xx")
|
||||
} else {
|
||||
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "xx")
|
||||
}
|
||||
}
|
||||
|
||||
_ = c(ctx, cmd)
|
||||
return cmd
|
||||
}
|
||||
|
Reference in New Issue
Block a user