Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for COPY command #2016

Merged
merged 1 commit into from Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions commands.go
Expand Up @@ -143,6 +143,7 @@ type Cmdable interface {
SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd
StrLen(ctx context.Context, key string) *IntCmd
Copy(ctx context.Context, sourceKey string, destKey string, db int, replace bool) *IntCmd

GetBit(ctx context.Context, key string, offset int64) *IntCmd
SetBit(ctx context.Context, key string, offset int64, value int) *IntCmd
Expand Down Expand Up @@ -1025,6 +1026,16 @@ func (c cmdable) StrLen(ctx context.Context, key string) *IntCmd {
return cmd
}

func (c cmdable) Copy(ctx context.Context, sourceKey, destKey string, db int, replace bool) *IntCmd {
args := []interface{}{"copy", sourceKey, destKey, "DB", db}
if replace {
args = append(args, "REPLACE")
}
cmd := NewIntCmd(ctx, args...)
_ = c(ctx, cmd)
return cmd
}

//------------------------------------------------------------------------------

func (c cmdable) GetBit(ctx context.Context, key string, offset int64) *IntCmd {
Expand Down
26 changes: 26 additions & 0 deletions commands_test.go
Expand Up @@ -1633,6 +1633,32 @@ var _ = Describe("Commands", func() {
Expect(strLen.Err()).NotTo(HaveOccurred())
Expect(strLen.Val()).To(Equal(int64(0)))
})

It("should Copy", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))

copy := client.Copy(ctx, "key", "newKey", redisOptions().DB, false)
Expect(copy.Err()).NotTo(HaveOccurred())
Expect(copy.Val()).To(Equal(int64(1)))

// Value is available by both keys now
getOld := client.Get(ctx, "key")
Expect(getOld.Err()).NotTo(HaveOccurred())
Expect(getOld.Val()).To(Equal("hello"))
getNew := client.Get(ctx, "newKey")
Expect(getNew.Err()).NotTo(HaveOccurred())
Expect(getNew.Val()).To(Equal("hello"))

// Overwriting an existing key should not succeed
overwrite := client.Copy(ctx, "newKey", "key", redisOptions().DB, false)
Expect(overwrite.Val()).To(Equal(int64(0)))

// Overwrite is allowed when replace=rue
replace := client.Copy(ctx, "newKey", "key", redisOptions().DB, true)
Expect(replace.Val()).To(Equal(int64(1)))
})
})

Describe("hashes", func() {
Expand Down