MCPcopy
hub / github.com/redis/go-redis / SetNX

Method SetNX

string_commands.go:604–621  ·  view source on GitHub ↗

SetNX sets the value of a key only if the key does not exist. Zero expiration means the key has no expiration time. KeepTTL is a Redis KEEPTTL option to keep existing TTL, it requires your redis-server version >= 6.0, otherwise you will receive an error: (error) ERR syntax error.

(ctx context.Context, key string, value interface{}, expiration time.Duration)

Source from the content-addressed store, hash-verified

602// KeepTTL is a Redis KEEPTTL option to keep existing TTL, it requires your redis-server version >= 6.0,
603// otherwise you will receive an error: (error) ERR syntax error.
604func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd {
605 var cmd *BoolCmd
606 switch expiration {
607 case 0:
608 cmd = NewBoolCmd(ctx, "set", key, value, "nx")
609 case KeepTTL:
610 cmd = NewBoolCmd(ctx, "set", key, value, "keepttl", "nx")
611 default:
612 if usePrecise(expiration) {
613 cmd = NewBoolCmd(ctx, "set", key, value, "px", formatMs(ctx, expiration), "nx")
614 } else {
615 cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "nx")
616 }
617 }
618
619 _ = c(ctx, cmd)
620 return cmd
621}
622
623// SetXX Redis `SET key value [expiration] XX` command.
624//

Callers

nothing calls this directly

Calls 4

NewBoolCmdFunction · 0.85
usePreciseFunction · 0.85
formatMsFunction · 0.85
formatSecFunction · 0.85

Tested by

no test coverage detected