Skip to content

Commit 04fff13

Browse files
committed
Add redis v7's ExpireNX, ExpireXX, ExpireGT, ExpireLT
1 parent 0260525 commit 04fff13

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

commands.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,42 @@ func (c cmdable) Exists(ctx context.Context, keys ...string) *IntCmd {
524524
return cmd
525525
}
526526

527-
func (c cmdable) Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
528-
cmd := NewBoolCmd(ctx, "expire", key, formatSec(ctx, expiration))
527+
func (c cmdable) expire(ctx context.Context, key string, expiration time.Duration, options ...interface{}) *BoolCmd {
528+
args := make([]interface{}, 3+len(options))
529+
args[0] = "expire"
530+
args[1] = key
531+
args[2] = formatSec(ctx, expiration)
532+
offset := 2
533+
534+
for i, option := range options {
535+
args[i+offset] = option
536+
}
537+
538+
cmd := NewBoolCmd(ctx, args...)
529539
_ = c(ctx, cmd)
530540
return cmd
531541
}
532542

543+
func (c cmdable) Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
544+
return c.expire(ctx, key, expiration)
545+
}
546+
547+
func (c cmdable) ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
548+
return c.expire(ctx, key, expiration, "NX")
549+
}
550+
551+
func (c cmdable) ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
552+
return c.expire(ctx, key, expiration, "XX")
553+
}
554+
555+
func (c cmdable) ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
556+
return c.expire(ctx, key, expiration, "GT")
557+
}
558+
559+
func (c cmdable) ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
560+
return c.expire(ctx, key, expiration, "LT")
561+
}
562+
533563
func (c cmdable) ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
534564
cmd := NewBoolCmd(ctx, "expireat", key, tm.Unix())
535565
_ = c(ctx, cmd)

0 commit comments

Comments
 (0)