This commit is contained in:
Vladimir Mihailenco 2020-09-18 14:57:59 +03:00
parent 10e779f856
commit 26d8b48cee
1 changed files with 14 additions and 12 deletions

View File

@ -9,8 +9,10 @@ import (
"github.com/go-redis/redis/v8/internal" "github.com/go-redis/redis/v8/internal"
) )
// KeepTTL used when set with keepttl option // KeepTTL is an option for Set command to keep key's existing TTL.
// Example: Set(ctx, key, value, redis.KeepTTL). // For example:
//
// rdb.Set(ctx, key, value, redis.KeepTTL)
const KeepTTL = -1 const KeepTTL = -1
func usePrecise(dur time.Duration) bool { func usePrecise(dur time.Duration) bool {
@ -757,10 +759,10 @@ func (c cmdable) MSetNX(ctx context.Context, values ...interface{}) *BoolCmd {
} }
// Redis `SET key value [expiration]` command. // Redis `SET key value [expiration]` command.
//
// Use expiration for `SETEX`-like behavior. // Use expiration for `SETEX`-like behavior.
//
// Zero expiration means the key has no expiration time. // Zero expiration means the key has no expiration time.
// KeepTTL(-1) expiration means command set adds keepttl option. // KeepTTL(-1) expiration is a Redis KEEPTTL option to keep existing TTL.
func (c cmdable) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd { func (c cmdable) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
args := make([]interface{}, 3, 5) args := make([]interface{}, 3, 5)
args[0] = "set" args[0] = "set"
@ -784,14 +786,14 @@ func (c cmdable) Set(ctx context.Context, key string, value interface{}, expirat
// Redis `SET key value [expiration] NX` command. // Redis `SET key value [expiration] NX` command.
// //
// Zero expiration means the key has no expiration time. // Zero expiration means the key has no expiration time.
// KeepTTL(-1) expiration means set command adds keepttl option. // KeepTTL(-1) expiration is a Redis KEEPTTL option to keep existing TTL.
func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd { func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd {
var cmd *BoolCmd var cmd *BoolCmd
switch { switch expiration {
case expiration == 0: case 0:
// Use old `SETNX` to support old Redis versions. // Use old `SETNX` to support old Redis versions.
cmd = NewBoolCmd(ctx, "setnx", key, value) cmd = NewBoolCmd(ctx, "setnx", key, value)
case expiration == KeepTTL: case KeepTTL:
cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "nx") cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "nx")
default: default:
if usePrecise(expiration) { if usePrecise(expiration) {
@ -808,13 +810,13 @@ func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expir
// Redis `SET key value [expiration] XX` command. // Redis `SET key value [expiration] XX` command.
// //
// Zero expiration means the key has no expiration time. // Zero expiration means the key has no expiration time.
// KeepTTL(-1) expiration means set command adds keepttl option. // KeepTTL(-1) expiration is a Redis KEEPTTL option to keep existing TTL.
func (c cmdable) SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd { func (c cmdable) SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd {
var cmd *BoolCmd var cmd *BoolCmd
switch { switch expiration {
case expiration == 0: case 0:
cmd = NewBoolCmd(ctx, "set", key, value, "xx") cmd = NewBoolCmd(ctx, "set", key, value, "xx")
case expiration == KeepTTL: case KeepTTL:
cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "xx") cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "xx")
default: default:
if usePrecise(expiration) { if usePrecise(expiration) {