Skip to content

Commit 7bd87c3

Browse files
authored
Add redis v7's ExpireAtNX, ExpireAtXX, ExpireAtGT, ExpireAtLT, PExpireNX, PExpireXX, PExpireGT, PExpireLT, PExpireAtNX, PExpireAtXX, PExpireAtGT, PExpireAtLT
feat: Add redis v7's NX, XX, GT, LT expireat, pexpire, pexpireat variants
1 parent 31ba855 commit 7bd87c3

File tree

1 file changed

+107
-10
lines changed

1 file changed

+107
-10
lines changed

commands.go

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,16 @@ type Cmdable interface {
175175
Dump(ctx context.Context, key string) *StringCmd
176176
Exists(ctx context.Context, keys ...string) *IntCmd
177177
Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
178-
ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
179-
ExpireTime(ctx context.Context, key string) *DurationCmd
180178
ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
181179
ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
182180
ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
183181
ExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
182+
ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
183+
ExpireAtNX(ctx context.Context, key string, tm time.Time) *BoolCmd
184+
ExpireAtXX(ctx context.Context, key string, tm time.Time) *BoolCmd
185+
ExpireAtGT(ctx context.Context, key string, tm time.Time) *BoolCmd
186+
ExpireAtLT(ctx context.Context, key string, tm time.Time) *BoolCmd
187+
ExpireTime(ctx context.Context, key string) *DurationCmd
184188
Keys(ctx context.Context, pattern string) *StringSliceCmd
185189
Migrate(ctx context.Context, host, port, key string, db int, timeout time.Duration) *StatusCmd
186190
Move(ctx context.Context, key string, db int) *BoolCmd
@@ -189,7 +193,15 @@ type Cmdable interface {
189193
ObjectIdleTime(ctx context.Context, key string) *DurationCmd
190194
Persist(ctx context.Context, key string) *BoolCmd
191195
PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
196+
PExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
197+
PExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
198+
PExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
199+
PExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
192200
PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
201+
PExpireAtNX(ctx context.Context, key string, tm time.Time) *BoolCmd
202+
PExpireAtXX(ctx context.Context, key string, tm time.Time) *BoolCmd
203+
PExpireAtGT(ctx context.Context, key string, tm time.Time) *BoolCmd
204+
PExpireAtLT(ctx context.Context, key string, tm time.Time) *BoolCmd
193205
PExpireTime(ctx context.Context, key string) *DurationCmd
194206
PTTL(ctx context.Context, key string) *DurationCmd
195207
RandomKey(ctx context.Context) *StringCmd
@@ -741,7 +753,37 @@ func (c cmdable) expire(
741753
}
742754

743755
func (c cmdable) ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
744-
cmd := NewBoolCmd(ctx, "expireat", key, tm.Unix())
756+
return c.expireAt(ctx, key, tm, "")
757+
}
758+
759+
func (c cmdable) ExpireAtNX(ctx context.Context, key string, tm time.Time) *BoolCmd {
760+
return c.expireAt(ctx, key, tm, "NX")
761+
}
762+
763+
func (c cmdable) ExpireAtXX(ctx context.Context, key string, tm time.Time) *BoolCmd {
764+
return c.expireAt(ctx, key, tm, "XX")
765+
}
766+
767+
func (c cmdable) ExpireAtGT(ctx context.Context, key string, tm time.Time) *BoolCmd {
768+
return c.expireAt(ctx, key, tm, "GT")
769+
}
770+
771+
func (c cmdable) ExpireAtLT(ctx context.Context, key string, tm time.Time) *BoolCmd {
772+
return c.expireAt(ctx, key, tm, "LT")
773+
}
774+
775+
func (c cmdable) expireAt(
776+
ctx context.Context, key string, tm time.Time, mode string,
777+
) *BoolCmd {
778+
args := make([]interface{}, 3, 4)
779+
args[0] = "expireat"
780+
args[1] = key
781+
args[2] = tm.Unix()
782+
if mode != "" {
783+
args = append(args, mode)
784+
}
785+
786+
cmd := NewBoolCmd(ctx, args...)
745787
_ = c(ctx, cmd)
746788
return cmd
747789
}
@@ -804,18 +846,73 @@ func (c cmdable) Persist(ctx context.Context, key string) *BoolCmd {
804846
}
805847

806848
func (c cmdable) PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
807-
cmd := NewBoolCmd(ctx, "pexpire", key, formatMs(ctx, expiration))
849+
return c.pexpire(ctx, key, expiration, "")
850+
}
851+
852+
func (c cmdable) PExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
853+
return c.pexpire(ctx, key, expiration, "NX")
854+
}
855+
856+
func (c cmdable) PExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
857+
return c.pexpire(ctx, key, expiration, "XX")
858+
}
859+
860+
func (c cmdable) PExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
861+
return c.pexpire(ctx, key, expiration, "GT")
862+
}
863+
864+
func (c cmdable) PExpireLT(ctx context.Context, key string, expiration time.Duration) *BoolCmd {
865+
return c.pexpire(ctx, key, expiration, "LT")
866+
}
867+
868+
func (c cmdable) pexpire(
869+
ctx context.Context, key string, expiration time.Duration, mode string,
870+
) *BoolCmd {
871+
args := make([]interface{}, 3, 4)
872+
args[0] = "pexpire"
873+
args[1] = key
874+
args[2] = formatMs(ctx, expiration)
875+
if mode != "" {
876+
args = append(args, mode)
877+
}
878+
879+
cmd := NewBoolCmd(ctx, args...)
808880
_ = c(ctx, cmd)
809881
return cmd
810882
}
811883

812884
func (c cmdable) PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd {
813-
cmd := NewBoolCmd(
814-
ctx,
815-
"pexpireat",
816-
key,
817-
tm.UnixNano()/int64(time.Millisecond),
818-
)
885+
return c.pexpireAt(ctx, key, tm, "")
886+
}
887+
888+
func (c cmdable) PExpireAtNX(ctx context.Context, key string, tm time.Time) *BoolCmd {
889+
return c.pexpireAt(ctx, key, tm, "NX")
890+
}
891+
892+
func (c cmdable) PExpireAtXX(ctx context.Context, key string, tm time.Time) *BoolCmd {
893+
return c.pexpireAt(ctx, key, tm, "XX")
894+
}
895+
896+
func (c cmdable) PExpireAtGT(ctx context.Context, key string, tm time.Time) *BoolCmd {
897+
return c.pexpireAt(ctx, key, tm, "GT")
898+
}
899+
900+
func (c cmdable) PExpireAtLT(ctx context.Context, key string, tm time.Time) *BoolCmd {
901+
return c.pexpireAt(ctx, key, tm, "LT")
902+
}
903+
904+
func (c cmdable) pexpireAt(
905+
ctx context.Context, key string, tm time.Time, mode string,
906+
) *BoolCmd {
907+
args := make([]interface{}, 3, 4)
908+
args[0] = "pexpireat"
909+
args[1] = key
910+
args[2] = tm.UnixNano() / int64(time.Millisecond)
911+
if mode != "" {
912+
args = append(args, mode)
913+
}
914+
915+
cmd := NewBoolCmd(ctx, args...)
819916
_ = c(ctx, cmd)
820917
return cmd
821918
}

0 commit comments

Comments
 (0)