Skip to content

Add redis v7's ExpireNX, ExpireXX, ExpireGT, ExpireLT #1928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,42 @@ func (c cmdable) Exists(ctx context.Context, keys ...string) *IntCmd {
return cmd
}

func (c cmdable) Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
cmd := NewBoolCmd(ctx, "expire", key, formatSec(ctx, expiration))
func (c cmdable) expire(ctx context.Context, key string, expiration time.Duration, options ...interface{}) *BoolCmd {
args := make([]interface{}, 3+len(options))
args[0] = "expire"
args[1] = key
args[2] = formatSec(ctx, expiration)
offset := 2

for i, option := range options {
args[i+offset] = option
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should args[i+3] ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove options altogether? They seems to be unused and using an interface{} is not a very efficient way to pass a string.

Instead just accept a string expire(ctx context.Context, key string, expiration time.Duration, mod string) and pass an empty string when there are no modifiers.

}

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

func (c cmdable) Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
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) ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
cmd := NewBoolCmd(ctx, "expireat", key, tm.Unix())
_ = c(ctx, cmd)
Expand Down