| 560 | } |
| 561 | |
| 562 | func buildKVSetArgs(key string, value []byte, opts KVSetOptions) ([]any, error) { |
| 563 | key = strings.TrimSpace(key) |
| 564 | if key == "" { |
| 565 | return nil, fmt.Errorf("home kv: key is empty") |
| 566 | } |
| 567 | if opts.EX > 0 && opts.PX > 0 { |
| 568 | return nil, fmt.Errorf("home kv: EX and PX are mutually exclusive") |
| 569 | } |
| 570 | if opts.EX < 0 || opts.PX < 0 { |
| 571 | return nil, fmt.Errorf("home kv: ttl must not be negative") |
| 572 | } |
| 573 | if opts.NX && opts.XX { |
| 574 | return nil, fmt.Errorf("home kv: NX and XX are mutually exclusive") |
| 575 | } |
| 576 | |
| 577 | args := []any{key, append([]byte(nil), value...)} |
| 578 | if opts.EX > 0 { |
| 579 | args = append(args, "EX", durationCeil(opts.EX, time.Second)) |
| 580 | } |
| 581 | if opts.PX > 0 { |
| 582 | args = append(args, "PX", durationCeil(opts.PX, time.Millisecond)) |
| 583 | } |
| 584 | if opts.NX { |
| 585 | args = append(args, "NX") |
| 586 | } |
| 587 | if opts.XX { |
| 588 | args = append(args, "XX") |
| 589 | } |
| 590 | return args, nil |
| 591 | } |
| 592 | |
| 593 | func durationCeil(value time.Duration, unit time.Duration) int64 { |
| 594 | if value <= 0 || unit <= 0 { |