Skip to content

Commit

Permalink
Add Replace operation
Browse files Browse the repository at this point in the history
  • Loading branch information
robfig committed Mar 4, 2013
1 parent 66c15b0 commit 2ec1fd6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ func (c *Client) add(rw *bufio.ReadWriter, item *Item) error {
return c.populateOne(rw, "add", item)
}

// Replace writes the given item, but only if the server *does*
// already hold data for this key
func (c *Client) Replace(item *Item) error {
return c.onItem(item, (*Client).replace)
}

func (c *Client) replace(rw *bufio.ReadWriter, item *Item) error {
return c.populateOne(rw, "replace", item)
}

// CompareAndSwap writes the given item that was previously returned
// by Get, if the value was neither modified or evicted between the
// Get and the CompareAndSwap calls. The item's Key should not change
Expand Down
8 changes: 8 additions & 0 deletions memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func testWithClient(t *testing.T, c *Client) {
t.Fatalf("second add(foo) want ErrNotStored, got %v", err)
}

// Replace
baz := &Item{Key: "baz", Value: []byte("bazvalue")}
if err := c.Replace(baz); err != ErrNotStored {
t.Fatalf("expected replace(baz) to return ErrNotStored, got %v", err)
}
err = c.Replace(bar)
checkErr(err, "replaced(foo): %v", err)

// GetMulti
m, err := c.GetMulti([]string{"foo", "bar"})
checkErr(err, "GetMulti: %v", err)
Expand Down

0 comments on commit 2ec1fd6

Please sign in to comment.