Skip to content

Commit

Permalink
Merge pull request #22 from whiteforestzx/add-expire-methods
Browse files Browse the repository at this point in the history
Add expire methods
  • Loading branch information
nvorobev authored Jul 22, 2024
2 parents 220d027 + 400b701 commit f88d2c5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 32 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f88d2c5

Please sign in to comment.