Skip to content

Commit f88d2c5

Browse files
authored
Merge pull request #22 from whiteforestzx/add-expire-methods
Add expire methods
2 parents 220d027 + 400b701 commit f88d2c5

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v3.3.1 (2024-22-07)
2+
3+
- Add ExpireNX, ExpireXX, ExpireGT, ExpireLT methods
4+
15
## v3.3.0 (2024-01-17)
26

37
- Rollback to stable version go-redis v9.0.3

pool.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,38 @@ func (p *Pool) Expire(ctx context.Context, key string, expiration time.Duration)
562562
return conn.Expire(ctx, key, expiration)
563563
}
564564

565+
func (p *Pool) ExpireNX(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd {
566+
conn, err := p.connFactory.getMasterConn(key)
567+
if err != nil {
568+
return newErrorBoolCmd(err)
569+
}
570+
return conn.ExpireNX(ctx, key, expiration)
571+
}
572+
573+
func (p *Pool) ExpireXX(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd {
574+
conn, err := p.connFactory.getMasterConn(key)
575+
if err != nil {
576+
return newErrorBoolCmd(err)
577+
}
578+
return conn.ExpireXX(ctx, key, expiration)
579+
}
580+
581+
func (p *Pool) ExpireGT(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd {
582+
conn, err := p.connFactory.getMasterConn(key)
583+
if err != nil {
584+
return newErrorBoolCmd(err)
585+
}
586+
return conn.ExpireGT(ctx, key, expiration)
587+
}
588+
589+
func (p *Pool) ExpireLT(ctx context.Context, key string, expiration time.Duration) *redis.BoolCmd {
590+
conn, err := p.connFactory.getMasterConn(key)
591+
if err != nil {
592+
return newErrorBoolCmd(err)
593+
}
594+
return conn.ExpireLT(ctx, key, expiration)
595+
}
596+
565597
// MExpire gives the result for each group of keys
566598
func (p *Pool) MExpire(ctx context.Context, expiration time.Duration, keys ...string) map[string]error {
567599
keyErrorsMap := func(results []redis.Cmder) map[string]error {

pool_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,49 @@ var _ = Describe("Pool", func() {
209209
}
210210
})
211211

212+
It("expire_nx", func() {
213+
key := "expirenx_key"
214+
for _, pool := range pools {
215+
Expect(pool.Set(ctx, key, "bar", 0).Val()).To(Equal("OK"))
216+
Expect(pool.Expire(ctx, key, 10*time.Second).Val()).To(Equal(true))
217+
Expect(pool.ExpireNX(ctx, key, 20*time.Second).Val()).To(Equal(false))
218+
_, _ = pool.Del(ctx, key)
219+
}
220+
})
221+
222+
It("expire_xx", func() {
223+
key := "expirexx_key"
224+
for _, pool := range pools {
225+
Expect(pool.Set(ctx, key, "bar", 0).Val()).To(Equal("OK"))
226+
Expect(pool.ExpireXX(ctx, key, 10*time.Second).Val()).To(Equal(false))
227+
Expect(pool.Expire(ctx, key, 10*time.Second).Val()).To(Equal(true))
228+
Expect(pool.ExpireXX(ctx, key, 20*time.Second).Val()).To(Equal(true))
229+
_, _ = pool.Del(ctx, key)
230+
}
231+
})
232+
233+
It("expire_gt", func() {
234+
key := "expiregt_key"
235+
for _, pool := range pools {
236+
Expect(pool.Set(ctx, key, "bar", 0).Val()).To(Equal("OK"))
237+
Expect(pool.Expire(ctx, key, 10*time.Second).Val()).To(Equal(true))
238+
Expect(pool.ExpireGT(ctx, key, time.Second).Val()).To(Equal(false))
239+
Expect(pool.ExpireGT(ctx, key, 20*time.Second).Val()).To(Equal(true))
240+
_, _ = pool.Del(ctx, key)
241+
}
242+
})
243+
244+
It("expire_lt", func() {
245+
key := "expirelt_key"
246+
for _, pool := range pools {
247+
Expect(pool.Set(ctx, key, "bar", 0).Val()).To(Equal("OK"))
248+
Expect(pool.Expire(ctx, key, 10*time.Second).Val()).To(Equal(true))
249+
Expect(pool.ExpireLT(ctx, key, 20*time.Second).Val()).To(Equal(false))
250+
Expect(pool.ExpireLT(ctx, key, time.Second).Val()).To(Equal(true))
251+
_, _ = pool.Del(ctx, key)
252+
}
253+
})
254+
212255
It("expire_at", func() {
213256
key := "expireat_foo"
214257
for _, pool := range pools {

0 commit comments

Comments
 (0)