mirror of https://github.com/go-redis/redis.git
Add SetEX command
This commit is contained in:
parent
38caa12762
commit
cc71f5d293
14
commands.go
14
commands.go
|
@ -124,6 +124,7 @@ type Cmdable interface {
|
||||||
MSet(ctx context.Context, values ...interface{}) *StatusCmd
|
MSet(ctx context.Context, values ...interface{}) *StatusCmd
|
||||||
MSetNX(ctx context.Context, values ...interface{}) *BoolCmd
|
MSetNX(ctx context.Context, values ...interface{}) *BoolCmd
|
||||||
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd
|
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd
|
||||||
|
SetEX(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd
|
||||||
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
|
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
|
||||||
SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
|
SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
|
||||||
SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd
|
SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd
|
||||||
|
@ -784,6 +785,19 @@ func (c cmdable) Set(ctx context.Context, key string, value interface{}, expirat
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Redis `SETEX key expiration value` command.
|
||||||
|
func (c cmdable) SetEX(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
|
||||||
|
args := make([]interface{}, 4)
|
||||||
|
args[0] = "setex"
|
||||||
|
args[1] = key
|
||||||
|
args[2] = formatSec(ctx, expiration)
|
||||||
|
args[3] = value
|
||||||
|
|
||||||
|
cmd := NewStatusCmd(ctx, args...)
|
||||||
|
_ = c(ctx, cmd)
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
// Redis `SET key value [expiration] NX` command.
|
// Redis `SET key value [expiration] NX` command.
|
||||||
//
|
//
|
||||||
// Zero expiration means the key has no expiration time.
|
// Zero expiration means the key has no expiration time.
|
||||||
|
|
|
@ -1147,7 +1147,7 @@ var _ = Describe("Commands", func() {
|
||||||
|
|
||||||
It("should Set with keepttl", func() {
|
It("should Set with keepttl", func() {
|
||||||
// set with ttl
|
// set with ttl
|
||||||
set := client.Set(ctx, "key", "hello", 5 * time.Second)
|
set := client.Set(ctx, "key", "hello", 5*time.Second)
|
||||||
Expect(set.Err()).NotTo(HaveOccurred())
|
Expect(set.Err()).NotTo(HaveOccurred())
|
||||||
Expect(set.Val()).To(Equal("OK"))
|
Expect(set.Val()).To(Equal("OK"))
|
||||||
|
|
||||||
|
@ -1172,6 +1172,19 @@ var _ = Describe("Commands", func() {
|
||||||
Expect(get.Val()).To(Equal("hello"))
|
Expect(get.Val()).To(Equal("hello"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("should SetEX", func() {
|
||||||
|
err := client.SetEX(ctx, "key", "hello", 100*time.Millisecond).Err()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
val, err := client.Get(ctx, "key").Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(val).To(Equal("hello"))
|
||||||
|
|
||||||
|
Eventually(func() error {
|
||||||
|
return client.Get(ctx, "foo").Err()
|
||||||
|
}, "1s", "100ms").Should(Equal(redis.Nil))
|
||||||
|
})
|
||||||
|
|
||||||
It("should SetNX", func() {
|
It("should SetNX", func() {
|
||||||
setNX := client.SetNX(ctx, "key", "hello", 0)
|
setNX := client.SetNX(ctx, "key", "hello", 0)
|
||||||
Expect(setNX.Err()).NotTo(HaveOccurred())
|
Expect(setNX.Err()).NotTo(HaveOccurred())
|
||||||
|
|
|
@ -188,6 +188,13 @@ func ExampleClient_Set() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleClient_SetEX() {
|
||||||
|
err := rdb.SetEX(ctx, "key", "value", time.Hour).Err()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleClient_Incr() {
|
func ExampleClient_Incr() {
|
||||||
result, err := rdb.Incr(ctx, "counter").Result()
|
result, err := rdb.Incr(ctx, "counter").Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue