Skip to content

Commit caee288

Browse files
authored
Merge pull request #1546 from TwinProduction/master
Add SetEX command
2 parents 38caa12 + b2df72a commit caee288

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

commands.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ type Cmdable interface {
124124
MSet(ctx context.Context, values ...interface{}) *StatusCmd
125125
MSetNX(ctx context.Context, values ...interface{}) *BoolCmd
126126
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd
127+
SetEX(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd
127128
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
128129
SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
129130
SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd
@@ -784,6 +785,13 @@ func (c cmdable) Set(ctx context.Context, key string, value interface{}, expirat
784785
return cmd
785786
}
786787

788+
// Redis `SETEX key expiration value` command.
789+
func (c cmdable) SetEX(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
790+
cmd := NewStatusCmd(ctx, "setex", key, formatSec(ctx, expiration), value)
791+
_ = c(ctx, cmd)
792+
return cmd
793+
}
794+
787795
// Redis `SET key value [expiration] NX` command.
788796
//
789797
// Zero expiration means the key has no expiration time.

commands_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ var _ = Describe("Commands", func() {
11471147

11481148
It("should Set with keepttl", func() {
11491149
// set with ttl
1150-
set := client.Set(ctx, "key", "hello", 5 * time.Second)
1150+
set := client.Set(ctx, "key", "hello", 5*time.Second)
11511151
Expect(set.Err()).NotTo(HaveOccurred())
11521152
Expect(set.Val()).To(Equal("OK"))
11531153

@@ -1172,6 +1172,19 @@ var _ = Describe("Commands", func() {
11721172
Expect(get.Val()).To(Equal("hello"))
11731173
})
11741174

1175+
It("should SetEX", func() {
1176+
err := client.SetEX(ctx, "key", "hello", 100*time.Millisecond).Err()
1177+
Expect(err).NotTo(HaveOccurred())
1178+
1179+
val, err := client.Get(ctx, "key").Result()
1180+
Expect(err).NotTo(HaveOccurred())
1181+
Expect(val).To(Equal("hello"))
1182+
1183+
Eventually(func() error {
1184+
return client.Get(ctx, "foo").Err()
1185+
}, "1s", "100ms").Should(Equal(redis.Nil))
1186+
})
1187+
11751188
It("should SetNX", func() {
11761189
setNX := client.SetNX(ctx, "key", "hello", 0)
11771190
Expect(setNX.Err()).NotTo(HaveOccurred())

example_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ func ExampleClient_Set() {
188188
}
189189
}
190190

191+
func ExampleClient_SetEX() {
192+
err := rdb.SetEX(ctx, "key", "value", time.Hour).Err()
193+
if err != nil {
194+
panic(err)
195+
}
196+
}
197+
191198
func ExampleClient_Incr() {
192199
result, err := rdb.Incr(ctx, "counter").Result()
193200
if err != nil {

0 commit comments

Comments
 (0)