Skip to content

Commit 730afbc

Browse files
authored
feat: add support for COPY command (#2016)
1 parent 627f4da commit 730afbc

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

commands.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ type Cmdable interface {
143143
SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
144144
SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd
145145
StrLen(ctx context.Context, key string) *IntCmd
146+
Copy(ctx context.Context, sourceKey string, destKey string, db int, replace bool) *IntCmd
146147

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

1029+
func (c cmdable) Copy(ctx context.Context, sourceKey, destKey string, db int, replace bool) *IntCmd {
1030+
args := []interface{}{"copy", sourceKey, destKey, "DB", db}
1031+
if replace {
1032+
args = append(args, "REPLACE")
1033+
}
1034+
cmd := NewIntCmd(ctx, args...)
1035+
_ = c(ctx, cmd)
1036+
return cmd
1037+
}
1038+
10281039
//------------------------------------------------------------------------------
10291040

10301041
func (c cmdable) GetBit(ctx context.Context, key string, offset int64) *IntCmd {

commands_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,32 @@ var _ = Describe("Commands", func() {
16331633
Expect(strLen.Err()).NotTo(HaveOccurred())
16341634
Expect(strLen.Val()).To(Equal(int64(0)))
16351635
})
1636+
1637+
It("should Copy", func() {
1638+
set := client.Set(ctx, "key", "hello", 0)
1639+
Expect(set.Err()).NotTo(HaveOccurred())
1640+
Expect(set.Val()).To(Equal("OK"))
1641+
1642+
copy := client.Copy(ctx, "key", "newKey", redisOptions().DB, false)
1643+
Expect(copy.Err()).NotTo(HaveOccurred())
1644+
Expect(copy.Val()).To(Equal(int64(1)))
1645+
1646+
// Value is available by both keys now
1647+
getOld := client.Get(ctx, "key")
1648+
Expect(getOld.Err()).NotTo(HaveOccurred())
1649+
Expect(getOld.Val()).To(Equal("hello"))
1650+
getNew := client.Get(ctx, "newKey")
1651+
Expect(getNew.Err()).NotTo(HaveOccurred())
1652+
Expect(getNew.Val()).To(Equal("hello"))
1653+
1654+
// Overwriting an existing key should not succeed
1655+
overwrite := client.Copy(ctx, "newKey", "key", redisOptions().DB, false)
1656+
Expect(overwrite.Val()).To(Equal(int64(0)))
1657+
1658+
// Overwrite is allowed when replace=rue
1659+
replace := client.Copy(ctx, "newKey", "key", redisOptions().DB, true)
1660+
Expect(replace.Val()).To(Equal(int64(1)))
1661+
})
16361662
})
16371663

16381664
Describe("hashes", func() {

0 commit comments

Comments
 (0)