diff --git a/CHANGELOG.md b/CHANGELOG.md index 56c8384..ece3937 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v3.3.1 (2024-22-07) + +- Add ExpireNX, ExpireXX, ExpireGT, ExpireLT methods + ## v3.3.0 (2024-01-17) - Rollback to stable version go-redis v9.0.3 diff --git a/pool.go b/pool.go index 3c38806..31ac56e 100644 --- a/pool.go +++ b/pool.go @@ -562,6 +562,38 @@ func (p *Pool) Expire(ctx context.Context, key string, expiration time.Duration) return conn.Expire(ctx, key, expiration) } +func (p *Pool) ExpireNX(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd { + conn, err := p.connFactory.getMasterConn(key) + if err != nil { + return newErrorBoolCmd(err) + } + return conn.ExpireNX(ctx, key, expiration) +} + +func (p *Pool) ExpireXX(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd { + conn, err := p.connFactory.getMasterConn(key) + if err != nil { + return newErrorBoolCmd(err) + } + return conn.ExpireXX(ctx, key, expiration) +} + +func (p *Pool) ExpireGT(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd { + conn, err := p.connFactory.getMasterConn(key) + if err != nil { + return newErrorBoolCmd(err) + } + return conn.ExpireGT(ctx, key, expiration) +} + +func (p *Pool) ExpireLT(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd { + conn, err := p.connFactory.getMasterConn(key) + if err != nil { + return newErrorBoolCmd(err) + } + return conn.ExpireLT(ctx, key, expiration) +} + // MExpire gives the result for each group of keys func (p *Pool) MExpire(ctx context.Context, expiration time.Duration, keys ...string) map[string]error { keyErrorsMap := func(results []redis.Cmder) map[string]error { diff --git a/pool_test.go b/pool_test.go index abcdae9..3f8416f 100644 --- a/pool_test.go +++ b/pool_test.go @@ -209,6 +209,49 @@ var _ = Describe("Pool", func() { } }) + It("expire_nx", func() { + key := "expirenx_key" + for _, pool := range pools { + Expect(pool.Set(ctx, key, "bar", 0).Val()).To(Equal("OK")) + Expect(pool.Expire(ctx, key, 10*time.Second).Val()).To(Equal(true)) + Expect(pool.ExpireNX(ctx, key, 20*time.Second).Val()).To(Equal(false)) + _, _ = pool.Del(ctx, key) + } + }) + + It("expire_xx", func() { + key := "expirexx_key" + for _, pool := range pools { + Expect(pool.Set(ctx, key, "bar", 0).Val()).To(Equal("OK")) + Expect(pool.ExpireXX(ctx, key, 10*time.Second).Val()).To(Equal(false)) + Expect(pool.Expire(ctx, key, 10*time.Second).Val()).To(Equal(true)) + Expect(pool.ExpireXX(ctx, key, 20*time.Second).Val()).To(Equal(true)) + _, _ = pool.Del(ctx, key) + } + }) + + It("expire_gt", func() { + key := "expiregt_key" + for _, pool := range pools { + Expect(pool.Set(ctx, key, "bar", 0).Val()).To(Equal("OK")) + Expect(pool.Expire(ctx, key, 10*time.Second).Val()).To(Equal(true)) + Expect(pool.ExpireGT(ctx, key, time.Second).Val()).To(Equal(false)) + Expect(pool.ExpireGT(ctx, key, 20*time.Second).Val()).To(Equal(true)) + _, _ = pool.Del(ctx, key) + } + }) + + It("expire_lt", func() { + key := "expirelt_key" + for _, pool := range pools { + Expect(pool.Set(ctx, key, "bar", 0).Val()).To(Equal("OK")) + Expect(pool.Expire(ctx, key, 10*time.Second).Val()).To(Equal(true)) + Expect(pool.ExpireLT(ctx, key, 20*time.Second).Val()).To(Equal(false)) + Expect(pool.ExpireLT(ctx, key, time.Second).Val()).To(Equal(true)) + _, _ = pool.Del(ctx, key) + } + }) + It("expire_at", func() { key := "expireat_foo" for _, pool := range pools {