Skip to content

Commit 9d5e485

Browse files
Expiretime and PExpireTime (#2426)
* Implemented EXPIRETIME and PEXPIRETIME
1 parent 74389af commit 9d5e485

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

commands.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ type Cmdable interface {
134134
Exists(ctx context.Context, keys ...string) *IntCmd
135135
Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
136136
ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
137+
ExpireTime(ctx context.Context, key string) *DurationCmd
137138
ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
138139
ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
139140
ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
@@ -147,6 +148,7 @@ type Cmdable interface {
147148
Persist(ctx context.Context, key string) *BoolCmd
148149
PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
149150
PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
151+
PExpireTime(ctx context.Context, key string) *DurationCmd
150152
PTTL(ctx context.Context, key string) *DurationCmd
151153
RandomKey(ctx context.Context) *StringCmd
152154
Rename(ctx context.Context, key, newkey string) *StatusCmd
@@ -624,6 +626,12 @@ func (c cmdable) ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCm
624626
return cmd
625627
}
626628

629+
func (c cmdable) ExpireTime(ctx context.Context, key string) *DurationCmd {
630+
cmd := NewDurationCmd(ctx, time.Second, "expiretime", key)
631+
_ = c(ctx, cmd)
632+
return cmd
633+
}
634+
627635
func (c cmdable) Keys(ctx context.Context, pattern string) *StringSliceCmd {
628636
cmd := NewStringSliceCmd(ctx, "keys", pattern)
629637
_ = c(ctx, cmd)
@@ -692,6 +700,12 @@ func (c cmdable) PExpireAt(ctx context.Context, key string, tm time.Time) *BoolC
692700
return cmd
693701
}
694702

703+
func (c cmdable) PExpireTime(ctx context.Context, key string) *DurationCmd {
704+
cmd := NewDurationCmd(ctx, time.Millisecond, "pexpiretime", key)
705+
_ = c(ctx, cmd)
706+
return cmd
707+
}
708+
695709
func (c cmdable) PTTL(ctx context.Context, key string) *DurationCmd {
696710
cmd := NewDurationCmd(ctx, time.Millisecond, "pttl", key)
697711
_ = c(ctx, cmd)

commands_test.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,33 @@ var _ = Describe("Commands", func() {
409409
})
410410

411411
It("should ExpireAt", func() {
412-
set := client.Set(ctx, "key", "Hello", 0)
413-
Expect(set.Err()).NotTo(HaveOccurred())
414-
Expect(set.Val()).To(Equal("OK"))
412+
setCmd := client.Set(ctx, "key", "Hello", 0)
413+
Expect(setCmd.Err()).NotTo(HaveOccurred())
414+
Expect(setCmd.Val()).To(Equal("OK"))
415415

416416
n, err := client.Exists(ctx, "key").Result()
417417
Expect(err).NotTo(HaveOccurred())
418418
Expect(n).To(Equal(int64(1)))
419419

420-
expireAt := client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
421-
Expect(expireAt.Err()).NotTo(HaveOccurred())
422-
Expect(expireAt.Val()).To(Equal(true))
420+
// Check correct expiration time is set in the future
421+
expireAt := time.Now().Add(time.Minute)
422+
expireAtCmd := client.ExpireAt(ctx, "key", expireAt)
423+
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
424+
Expect(expireAtCmd.Val()).To(Equal(true))
425+
426+
timeCmd := client.ExpireTime(ctx, "key")
427+
Expect(timeCmd.Err()).NotTo(HaveOccurred())
428+
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
429+
430+
// Check correct expiration in the past
431+
expireAtCmd = client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
432+
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
433+
Expect(expireAtCmd.Val()).To(Equal(true))
423434

424435
n, err = client.Exists(ctx, "key").Result()
425436
Expect(err).NotTo(HaveOccurred())
426437
Expect(n).To(Equal(int64(0)))
438+
427439
})
428440

429441
It("should Keys", func() {
@@ -569,6 +581,28 @@ var _ = Describe("Commands", func() {
569581
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
570582
})
571583

584+
It("should PExpireTime", func() {
585+
586+
// The command returns -1 if the key exists but has no associated expiration time.
587+
// The command returns -2 if the key does not exist.
588+
pExpireTime := client.PExpireTime(ctx, "key")
589+
Expect(pExpireTime.Err()).NotTo(HaveOccurred())
590+
Expect(pExpireTime.Val() < 0).To(Equal(true))
591+
592+
set := client.Set(ctx, "key", "hello", 0)
593+
Expect(set.Err()).NotTo(HaveOccurred())
594+
Expect(set.Val()).To(Equal("OK"))
595+
596+
timestamp := time.Now().Add(time.Minute)
597+
expireAt := client.PExpireAt(ctx, "key", timestamp)
598+
Expect(expireAt.Err()).NotTo(HaveOccurred())
599+
Expect(expireAt.Val()).To(Equal(true))
600+
601+
pExpireTime = client.PExpireTime(ctx, "key")
602+
Expect(pExpireTime.Err()).NotTo(HaveOccurred())
603+
Expect(pExpireTime.Val().Milliseconds()).To(BeNumerically("==", timestamp.UnixMilli()))
604+
})
605+
572606
It("should PTTL", func() {
573607
set := client.Set(ctx, "key", "Hello", 0)
574608
Expect(set.Err()).NotTo(HaveOccurred())
@@ -786,7 +820,32 @@ var _ = Describe("Commands", func() {
786820
Expect(touch.Val()).To(Equal(int64(2)))
787821
})
788822

823+
It("should ExpireTime", func() {
824+
825+
// The command returns -1 if the key exists but has no associated expiration time.
826+
// The command returns -2 if the key does not exist.
827+
expireTimeCmd := client.ExpireTime(ctx, "key")
828+
Expect(expireTimeCmd.Err()).NotTo(HaveOccurred())
829+
Expect(expireTimeCmd.Val() < 0).To(Equal(true))
830+
831+
set := client.Set(ctx, "key", "hello", 0)
832+
Expect(set.Err()).NotTo(HaveOccurred())
833+
Expect(set.Val()).To(Equal("OK"))
834+
835+
expireAt := time.Now().Add(time.Minute)
836+
expireAtCmd := client.ExpireAt(ctx, "key", expireAt)
837+
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
838+
Expect(expireAtCmd.Val()).To(Equal(true))
839+
840+
expireTimeCmd = client.ExpireTime(ctx, "key")
841+
Expect(expireTimeCmd.Err()).NotTo(HaveOccurred())
842+
Expect(expireTimeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
843+
})
844+
789845
It("should TTL", func() {
846+
847+
// The command returns -1 if the key exists but has no associated expire
848+
// The command returns -2 if the key does not exist.
790849
ttl := client.TTL(ctx, "key")
791850
Expect(ttl.Err()).NotTo(HaveOccurred())
792851
Expect(ttl.Val() < 0).To(Equal(true))

0 commit comments

Comments
 (0)