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)
| 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. |
| 604 | func (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 | // |
nothing calls this directly
no test coverage detected