forked from mirror/redis
Tweak API
This commit is contained in:
parent
7b7f9d6e0e
commit
61680f373c
|
@ -807,16 +807,17 @@ type SetArgs struct {
|
|||
// SetArgs supports all the options that the SET command supports.
|
||||
// It is the alternative to the Set function when you want
|
||||
// to have more control over the options.
|
||||
func (c cmdable) SetArgs(ctx context.Context, key string, value interface{}, a *SetArgs) *StatusCmd {
|
||||
func (c cmdable) SetArgs(ctx context.Context, key string, value interface{}, a SetArgs) *StatusCmd {
|
||||
args := []interface{}{"set", key, value}
|
||||
|
||||
if a.KeepTTL {
|
||||
args = append(args, "keepttl")
|
||||
}
|
||||
|
||||
if !a.ExpireAt.IsZero() && !a.KeepTTL {
|
||||
if !a.ExpireAt.IsZero() {
|
||||
args = append(args, "exat", a.ExpireAt.Unix())
|
||||
} else if a.TTL > 0 && !a.KeepTTL {
|
||||
}
|
||||
if a.TTL > 0 {
|
||||
if usePrecise(a.TTL) {
|
||||
args = append(args, "px", formatMs(ctx, a.TTL))
|
||||
} else {
|
||||
|
|
|
@ -1161,7 +1161,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with TTL", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
TTL: 500 * time.Millisecond,
|
||||
}
|
||||
err := client.SetArgs(ctx, "key", "hello", args).Err()
|
||||
|
@ -1178,7 +1178,7 @@ var _ = Describe("Commands", func() {
|
|||
|
||||
It("should SetWithArgs with expiration date", func() {
|
||||
expireAt := time.Now().AddDate(1, 1, 1)
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
ExpireAt: expireAt,
|
||||
}
|
||||
err := client.SetArgs(ctx, "key", "hello", args).Err()
|
||||
|
@ -1196,7 +1196,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with negative expiration date", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
ExpireAt: time.Now().AddDate(-3, 1, 1),
|
||||
}
|
||||
// redis accepts a timestamp less than the current date
|
||||
|
@ -1211,7 +1211,7 @@ var _ = Describe("Commands", func() {
|
|||
|
||||
It("should SetWithArgs with keepttl", func() {
|
||||
// Set with ttl
|
||||
argsWithTTL := &redis.SetArgs{
|
||||
argsWithTTL := redis.SetArgs{
|
||||
TTL: 5 * time.Second,
|
||||
}
|
||||
set := client.SetArgs(ctx, "key", "hello", argsWithTTL)
|
||||
|
@ -1219,7 +1219,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(set.Result()).To(Equal("OK"))
|
||||
|
||||
// Set with keepttl
|
||||
argsWithKeepTTL := &redis.SetArgs{
|
||||
argsWithKeepTTL := redis.SetArgs{
|
||||
KeepTTL: true,
|
||||
}
|
||||
set = client.SetArgs(ctx, "key", "hello", argsWithKeepTTL)
|
||||
|
@ -1236,7 +1236,7 @@ var _ = Describe("Commands", func() {
|
|||
err := client.Set(ctx, "key", "hello", 0).Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Mode: "nx",
|
||||
}
|
||||
val, err := client.SetArgs(ctx, "key", "hello", args).Result()
|
||||
|
@ -1245,7 +1245,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with NX mode and key does not exist", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Mode: "nx",
|
||||
}
|
||||
val, err := client.SetArgs(ctx, "key", "hello", args).Result()
|
||||
|
@ -1254,7 +1254,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with NX mode and GET option", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Mode: "nx",
|
||||
Get: true,
|
||||
}
|
||||
|
@ -1264,7 +1264,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with expiration, NX mode, and key does not exist", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
TTL: 500 * time.Millisecond,
|
||||
Mode: "nx",
|
||||
}
|
||||
|
@ -1281,7 +1281,7 @@ var _ = Describe("Commands", func() {
|
|||
e := client.Set(ctx, "key", "hello", 0)
|
||||
Expect(e.Err()).NotTo(HaveOccurred())
|
||||
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
TTL: 500 * time.Millisecond,
|
||||
Mode: "nx",
|
||||
}
|
||||
|
@ -1291,7 +1291,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with expiration, NX mode, and GET option", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
TTL: 500 * time.Millisecond,
|
||||
Mode: "nx",
|
||||
Get: true,
|
||||
|
@ -1302,7 +1302,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with XX mode and key does not exist", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Mode: "xx",
|
||||
}
|
||||
val, err := client.SetArgs(ctx, "key", "world", args).Result()
|
||||
|
@ -1314,7 +1314,7 @@ var _ = Describe("Commands", func() {
|
|||
e := client.Set(ctx, "key", "hello", 0).Err()
|
||||
Expect(e).NotTo(HaveOccurred())
|
||||
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Mode: "xx",
|
||||
}
|
||||
val, err := client.SetArgs(ctx, "key", "world", args).Result()
|
||||
|
@ -1326,7 +1326,7 @@ var _ = Describe("Commands", func() {
|
|||
e := client.Set(ctx, "key", "hello", 0).Err()
|
||||
Expect(e).NotTo(HaveOccurred())
|
||||
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Mode: "xx",
|
||||
Get: true,
|
||||
}
|
||||
|
@ -1336,7 +1336,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with XX mode and GET option, and key does not exist", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Mode: "xx",
|
||||
Get: true,
|
||||
}
|
||||
|
@ -1347,7 +1347,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with expiration, XX mode, GET option, and key does not exist", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
TTL: 500 * time.Millisecond,
|
||||
Mode: "xx",
|
||||
Get: true,
|
||||
|
@ -1362,7 +1362,7 @@ var _ = Describe("Commands", func() {
|
|||
e := client.Set(ctx, "key", "hello", 0)
|
||||
Expect(e.Err()).NotTo(HaveOccurred())
|
||||
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
TTL: 500 * time.Millisecond,
|
||||
Mode: "xx",
|
||||
Get: true,
|
||||
|
@ -1378,7 +1378,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetWithArgs with Get and key does not exist yet", func() {
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Get: true,
|
||||
}
|
||||
|
||||
|
@ -1391,7 +1391,7 @@ var _ = Describe("Commands", func() {
|
|||
e := client.Set(ctx, "key", "hello", 0)
|
||||
Expect(e.Err()).NotTo(HaveOccurred())
|
||||
|
||||
args := &redis.SetArgs{
|
||||
args := redis.SetArgs{
|
||||
Get: true,
|
||||
}
|
||||
|
||||
|
|
2
go.mod
2
go.mod
|
@ -8,4 +8,6 @@ require (
|
|||
github.com/onsi/ginkgo v1.15.0
|
||||
github.com/onsi/gomega v1.10.5
|
||||
go.opentelemetry.io/otel v0.17.0
|
||||
go.opentelemetry.io/otel/metric v0.17.0
|
||||
go.opentelemetry.io/otel/trace v0.17.0
|
||||
)
|
||||
|
|
16
go.sum
16
go.sum
|
@ -34,11 +34,17 @@ github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7
|
|||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
go.opentelemetry.io/otel v0.16.0 h1:uIWEbdeb4vpKPGITLsRVUS44L5oDbDUCZxn8lkxhmgw=
|
||||
go.opentelemetry.io/otel v0.16.0/go.mod h1:e4GKElweB8W2gWUqbghw0B8t5MCTccc9212eNHnOHwA=
|
||||
go.opentelemetry.io/otel v0.17.0 h1:6MKOu8WY4hmfpQ4oQn34u6rYhnf2sWf1LXYO/UFm71U=
|
||||
go.opentelemetry.io/otel v0.17.0/go.mod h1:Oqtdxmf7UtEvL037ohlgnaYa1h7GtMh0NcSd9eqkC9s=
|
||||
go.opentelemetry.io/otel/metric v0.17.0 h1:t+5EioN8YFXQ2EH+1j6FHCKMUj+57zIDSnSGr/mWuug=
|
||||
go.opentelemetry.io/otel/metric v0.17.0/go.mod h1:hUz9lH1rNXyEwWAhIWCMFWKhYtpASgSnObJFnU26dJ0=
|
||||
go.opentelemetry.io/otel/oteltest v0.17.0 h1:TyAihUowTDLqb4+m5ePAsR71xPJaTBJl4KDArIdi9k4=
|
||||
go.opentelemetry.io/otel/oteltest v0.17.0/go.mod h1:JT/LGFxPwpN+nlsTiinSYjdIx3hZIGqHCpChcIZmdoE=
|
||||
go.opentelemetry.io/otel/trace v0.17.0 h1:SBOj64/GAOyWzs5F680yW1ITIfJkm6cJWL2YAvuL9xY=
|
||||
go.opentelemetry.io/otel/trace v0.17.0/go.mod h1:bIujpqg6ZL6xUTubIUgziI1jSaUPthmabA/ygf/6Cfg=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
|
@ -60,7 +66,6 @@ golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210112080510-489259a85091 h1:DMyOG0U+gKfu8JZzg2UQe9MeaC1X+xQWlAKcRnjxjCw=
|
||||
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
@ -72,7 +77,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
|
|||
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
|
Loading…
Reference in New Issue