Skip to content

Commit

Permalink
feat: Add redis v7's NX, XX, GT, LT expire variants
Browse files Browse the repository at this point in the history
  • Loading branch information
DaemonSnake authored and vmihailenco committed Nov 21, 2021
1 parent 5aefa73 commit e19bbb2
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,37 @@ func (c cmdable) Exists(ctx context.Context, keys ...string) *IntCmd {
}

func (c cmdable) Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
cmd := NewBoolCmd(ctx, "expire", key, formatSec(ctx, expiration))
return c.expire(ctx, key, expiration, "")
}

func (c cmdable) ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
return c.expire(ctx, key, expiration, "NX")
}

func (c cmdable) ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
return c.expire(ctx, key, expiration, "XX")
}

func (c cmdable) ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
return c.expire(ctx, key, expiration, "GT")
}

func (c cmdable) ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
return c.expire(ctx, key, expiration, "LT")
}

func (c cmdable) expire(
ctx context.Context, key string, expiration time.Duration, mode string,
) *BoolCmd {
args := make([]interface{}, 3, 4)
args[0] = "expire"
args[1] = key
args[2] = formatSec(ctx, expiration)
if mode != "" {
args = append(args, mode)
}

cmd := NewBoolCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}
Expand Down

0 comments on commit e19bbb2

Please sign in to comment.