Expiretime and PExpireTime (#2426)

* Implemented EXPIRETIME and PEXPIRETIME
This commit is contained in:
Elena Kolevska 2023-02-07 10:41:27 +00:00 committed by GitHub
parent 74389af269
commit 9d5e48507e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 6 deletions

View File

@ -134,6 +134,7 @@ type Cmdable interface {
Exists(ctx context.Context, keys ...string) *IntCmd Exists(ctx context.Context, keys ...string) *IntCmd
Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd Expire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd ExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
ExpireTime(ctx context.Context, key string) *DurationCmd
ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd ExpireNX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd ExpireXX(ctx context.Context, key string, expiration time.Duration) *BoolCmd
ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd ExpireGT(ctx context.Context, key string, expiration time.Duration) *BoolCmd
@ -147,6 +148,7 @@ type Cmdable interface {
Persist(ctx context.Context, key string) *BoolCmd Persist(ctx context.Context, key string) *BoolCmd
PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd PExpire(ctx context.Context, key string, expiration time.Duration) *BoolCmd
PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd PExpireAt(ctx context.Context, key string, tm time.Time) *BoolCmd
PExpireTime(ctx context.Context, key string) *DurationCmd
PTTL(ctx context.Context, key string) *DurationCmd PTTL(ctx context.Context, key string) *DurationCmd
RandomKey(ctx context.Context) *StringCmd RandomKey(ctx context.Context) *StringCmd
Rename(ctx context.Context, key, newkey string) *StatusCmd 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
return cmd return cmd
} }
func (c cmdable) ExpireTime(ctx context.Context, key string) *DurationCmd {
cmd := NewDurationCmd(ctx, time.Second, "expiretime", key)
_ = c(ctx, cmd)
return cmd
}
func (c cmdable) Keys(ctx context.Context, pattern string) *StringSliceCmd { func (c cmdable) Keys(ctx context.Context, pattern string) *StringSliceCmd {
cmd := NewStringSliceCmd(ctx, "keys", pattern) cmd := NewStringSliceCmd(ctx, "keys", pattern)
_ = c(ctx, cmd) _ = c(ctx, cmd)
@ -692,6 +700,12 @@ func (c cmdable) PExpireAt(ctx context.Context, key string, tm time.Time) *BoolC
return cmd return cmd
} }
func (c cmdable) PExpireTime(ctx context.Context, key string) *DurationCmd {
cmd := NewDurationCmd(ctx, time.Millisecond, "pexpiretime", key)
_ = c(ctx, cmd)
return cmd
}
func (c cmdable) PTTL(ctx context.Context, key string) *DurationCmd { func (c cmdable) PTTL(ctx context.Context, key string) *DurationCmd {
cmd := NewDurationCmd(ctx, time.Millisecond, "pttl", key) cmd := NewDurationCmd(ctx, time.Millisecond, "pttl", key)
_ = c(ctx, cmd) _ = c(ctx, cmd)

View File

@ -409,21 +409,33 @@ var _ = Describe("Commands", func() {
}) })
It("should ExpireAt", func() { It("should ExpireAt", func() {
set := client.Set(ctx, "key", "Hello", 0) setCmd := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred()) Expect(setCmd.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK")) Expect(setCmd.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key").Result() n, err := client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1))) Expect(n).To(Equal(int64(1)))
expireAt := client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour)) // Check correct expiration time is set in the future
Expect(expireAt.Err()).NotTo(HaveOccurred()) expireAt := time.Now().Add(time.Minute)
Expect(expireAt.Val()).To(Equal(true)) expireAtCmd := client.ExpireAt(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
timeCmd := client.ExpireTime(ctx, "key")
Expect(timeCmd.Err()).NotTo(HaveOccurred())
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
// Check correct expiration in the past
expireAtCmd = client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
n, err = client.Exists(ctx, "key").Result() n, err = client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0))) Expect(n).To(Equal(int64(0)))
}) })
It("should Keys", func() { It("should Keys", func() {
@ -569,6 +581,28 @@ var _ = Describe("Commands", func() {
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond)) Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
}) })
It("should PExpireTime", func() {
// The command returns -1 if the key exists but has no associated expiration time.
// The command returns -2 if the key does not exist.
pExpireTime := client.PExpireTime(ctx, "key")
Expect(pExpireTime.Err()).NotTo(HaveOccurred())
Expect(pExpireTime.Val() < 0).To(Equal(true))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
timestamp := time.Now().Add(time.Minute)
expireAt := client.PExpireAt(ctx, "key", timestamp)
Expect(expireAt.Err()).NotTo(HaveOccurred())
Expect(expireAt.Val()).To(Equal(true))
pExpireTime = client.PExpireTime(ctx, "key")
Expect(pExpireTime.Err()).NotTo(HaveOccurred())
Expect(pExpireTime.Val().Milliseconds()).To(BeNumerically("==", timestamp.UnixMilli()))
})
It("should PTTL", func() { It("should PTTL", func() {
set := client.Set(ctx, "key", "Hello", 0) set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred()) Expect(set.Err()).NotTo(HaveOccurred())
@ -786,7 +820,32 @@ var _ = Describe("Commands", func() {
Expect(touch.Val()).To(Equal(int64(2))) Expect(touch.Val()).To(Equal(int64(2)))
}) })
It("should ExpireTime", func() {
// The command returns -1 if the key exists but has no associated expiration time.
// The command returns -2 if the key does not exist.
expireTimeCmd := client.ExpireTime(ctx, "key")
Expect(expireTimeCmd.Err()).NotTo(HaveOccurred())
Expect(expireTimeCmd.Val() < 0).To(Equal(true))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expireAt := time.Now().Add(time.Minute)
expireAtCmd := client.ExpireAt(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
expireTimeCmd = client.ExpireTime(ctx, "key")
Expect(expireTimeCmd.Err()).NotTo(HaveOccurred())
Expect(expireTimeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
})
It("should TTL", func() { It("should TTL", func() {
// The command returns -1 if the key exists but has no associated expire
// The command returns -2 if the key does not exist.
ttl := client.TTL(ctx, "key") ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred()) Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val() < 0).To(Equal(true)) Expect(ttl.Val() < 0).To(Equal(true))