Skip to content

Commit e19bbb2

Browse files
DaemonSnakevmihailenco
authored andcommitted
feat: Add redis v7's NX, XX, GT, LT expire variants
1 parent 5aefa73 commit e19bbb2

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

commands.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,37 @@ func (c cmdable) Exists(ctx context.Context, keys ...string) *IntCmd {
525525
}
526526

527527
func (c cmdable) Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
528-
cmd := NewBoolCmd(ctx, "expire", key, formatSec(ctx, expiration))
528+
return c.expire(ctx, key, expiration, "")
529+
}
530+
531+
func (c cmdable) ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
532+
return c.expire(ctx, key, expiration, "NX")
533+
}
534+
535+
func (c cmdable) ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
536+
return c.expire(ctx, key, expiration, "XX")
537+
}
538+
539+
func (c cmdable) ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
540+
return c.expire(ctx, key, expiration, "GT")
541+
}
542+
543+
func (c cmdable) ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
544+
return c.expire(ctx, key, expiration, "LT")
545+
}
546+
547+
func (c cmdable) expire(
548+
ctx context.Context, key string, expiration time.Duration, mode string,
549+
) *BoolCmd {
550+
args := make([]interface{}, 3, 4)
551+
args[0] = "expire"
552+
args[1] = key
553+
args[2] = formatSec(ctx, expiration)
554+
if mode != "" {
555+
args = append(args, mode)
556+
}
557+
558+
cmd := NewBoolCmd(ctx, args...)
529559
_ = c(ctx, cmd)
530560
return cmd
531561
}

0 commit comments

Comments
 (0)