Skip to content

Commit 52eb7da

Browse files
author
Alex Kahn
committed
Add support for COPY command
1 parent a034b08 commit 52eb7da

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

commands.go

Lines changed: 17 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,22 @@ 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+
var replaceArg string
1031+
if replace {
1032+
replaceArg = "REPLACE"
1033+
}
1034+
cmd := NewIntCmd(ctx,
1035+
"copy",
1036+
sourceKey,
1037+
destKey,
1038+
db,
1039+
replaceArg,
1040+
)
1041+
_ = c(ctx, cmd)
1042+
return cmd
1043+
}
1044+
10281045
//------------------------------------------------------------------------------
10291046

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

commands_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,35 @@ 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", 0, false)
1643+
Expect(copy.Err()).NotTo(HaveOccurred())
1644+
Expect(copy.Val()).To(Equal(int64(1)))
1645+
1646+
getOld := client.Get(ctx, "key")
1647+
Expect(getOld.Err()).NotTo(HaveOccurred())
1648+
Expect(getOld.Result()).To(Equal(redis.Nil))
1649+
1650+
getNew := client.Get(ctx, "newKey")
1651+
Expect(getNew.Err()).NotTo(HaveOccurred())
1652+
Expect(getNew.Result()).To(Equal("hello"))
1653+
1654+
// Copy over existing key should return error
1655+
set = client.Set(ctx, "key", "hello", 0)
1656+
Expect(set.Err()).NotTo(HaveOccurred())
1657+
Expect(set.Val()).To(Equal("OK"))
1658+
overwrite := client.Copy(ctx, "newKey", "key", 0, false)
1659+
Expect(overwrite.Err()).To(HaveOccurred())
1660+
1661+
// Overwrite is allowed when replace=rue
1662+
replace := client.Copy(ctx, "newKey", "key", 0, true)
1663+
Expect(replace.Err()).NotTo(HaveOccurred())
1664+
})
16361665
})
16371666

16381667
Describe("hashes", func() {

0 commit comments

Comments
 (0)