Skip to content

Commit f7880d5

Browse files
vanstinatorbradfitz
authored andcommitted
feat: add support for append/prepend
1 parent fb4bf63 commit f7880d5

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

memcache/memcache.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,26 @@ func (c *Client) replace(rw *bufio.ReadWriter, item *Item) error {
561561
return c.populateOne(rw, "replace", item)
562562
}
563563

564+
// Append appends the given item to the existing item, if a value already
565+
// exists for its key. ErrNotStored is returned if that condition is not met.
566+
func (c *Client) Append(item *Item) error {
567+
return c.onItem(item, (*Client).append)
568+
}
569+
570+
func (c *Client) append(rw *bufio.ReadWriter, item *Item) error {
571+
return c.populateOne(rw, "append", item)
572+
}
573+
574+
// Prepend prepends the given item to the existing item, if a value already
575+
// exists for its key. ErrNotStored is returned if that condition is not met.
576+
func (c *Client) Prepend(item *Item) error {
577+
return c.onItem(item, (*Client).prepend)
578+
}
579+
580+
func (c *Client) prepend(rw *bufio.ReadWriter, item *Item) error {
581+
return c.populateOne(rw, "prepend", item)
582+
}
583+
564584
// CompareAndSwap writes the given item that was previously returned
565585
// by Get, if the value was neither modified or evicted between the
566586
// Get and the CompareAndSwap calls. The item's Key should not change

memcache/memcache_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,34 @@ func testWithClient(t *testing.T, c *Client) {
141141
t.Fatalf("second add(foo) want ErrNotStored, got %v", err)
142142
}
143143

144+
// Append
145+
append := &Item{Key: "append", Value: []byte("appendval")}
146+
if err := c.Append(append); err != ErrNotStored {
147+
t.Fatalf("first append(append) want ErrNotStored, got %v", err)
148+
}
149+
c.Set(append)
150+
err = c.Append(&Item{Key: "append", Value: []byte("1")})
151+
checkErr(err, "second append(append): %v", err)
152+
appended, err := c.Get("append")
153+
checkErr(err, "third append(append): %v", err)
154+
if string(appended.Value) != string(append.Value)+"1" {
155+
t.Fatalf("Append: want=append1, got=%s", string(appended.Value))
156+
}
157+
158+
// Prepend
159+
prepend := &Item{Key: "prepend", Value: []byte("prependval")}
160+
if err := c.Prepend(prepend); err != ErrNotStored {
161+
t.Fatalf("first prepend(prepend) want ErrNotStored, got %v", err)
162+
}
163+
c.Set(prepend)
164+
err = c.Prepend(&Item{Key: "prepend", Value: []byte("1")})
165+
checkErr(err, "second prepend(prepend): %v", err)
166+
prepended, err := c.Get("prepend")
167+
checkErr(err, "third prepend(prepend): %v", err)
168+
if string(prepended.Value) != "1"+string(prepend.Value) {
169+
t.Fatalf("Prepend: want=1prepend, got=%s", string(prepended.Value))
170+
}
171+
144172
// Replace
145173
baz := &Item{Key: "baz", Value: []byte("bazvalue")}
146174
if err := c.Replace(baz); err != ErrNotStored {

0 commit comments

Comments
 (0)