mirror of https://github.com/go-redis/redis.git
Expiretime and PExpireTime (#2426)
* Implemented EXPIRETIME and PEXPIRETIME
This commit is contained in:
parent
74389af269
commit
9d5e48507e
14
commands.go
14
commands.go
|
@ -134,6 +134,7 @@ type Cmdable interface {
|
|||
Exists(ctx context.Context, keys ...string) *IntCmd
|
||||
Expire(ctx context.Context, key string, expiration time.Duration) *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
|
||||
ExpireXX(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
|
||||
PExpire(ctx context.Context, key string, expiration time.Duration) *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
|
||||
RandomKey(ctx context.Context) *StringCmd
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
cmd := NewStringSliceCmd(ctx, "keys", pattern)
|
||||
_ = c(ctx, cmd)
|
||||
|
@ -692,6 +700,12 @@ func (c cmdable) PExpireAt(ctx context.Context, key string, tm time.Time) *BoolC
|
|||
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 {
|
||||
cmd := NewDurationCmd(ctx, time.Millisecond, "pttl", key)
|
||||
_ = c(ctx, cmd)
|
||||
|
|
|
@ -409,21 +409,33 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should ExpireAt", func() {
|
||||
set := client.Set(ctx, "key", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
setCmd := client.Set(ctx, "key", "Hello", 0)
|
||||
Expect(setCmd.Err()).NotTo(HaveOccurred())
|
||||
Expect(setCmd.Val()).To(Equal("OK"))
|
||||
|
||||
n, err := client.Exists(ctx, "key").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(n).To(Equal(int64(1)))
|
||||
|
||||
expireAt := client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
|
||||
Expect(expireAt.Err()).NotTo(HaveOccurred())
|
||||
Expect(expireAt.Val()).To(Equal(true))
|
||||
// Check correct expiration time is set in the future
|
||||
expireAt := time.Now().Add(time.Minute)
|
||||
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()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(n).To(Equal(int64(0)))
|
||||
|
||||
})
|
||||
|
||||
It("should Keys", func() {
|
||||
|
@ -569,6 +581,28 @@ var _ = Describe("Commands", func() {
|
|||
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() {
|
||||
set := client.Set(ctx, "key", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
|
@ -786,7 +820,32 @@ var _ = Describe("Commands", func() {
|
|||
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() {
|
||||
|
||||
// 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")
|
||||
Expect(ttl.Err()).NotTo(HaveOccurred())
|
||||
Expect(ttl.Val() < 0).To(Equal(true))
|
||||
|
|
Loading…
Reference in New Issue