Rob Figueiredo преди 12 години
родител
ревизия
2ec1fd6256
променени са 2 файла, в които са добавени 18 реда и са изтрити 0 реда
  1. 10 0
      memcache/memcache.go
  2. 8 0
      memcache/memcache_test.go

+ 10 - 0
memcache/memcache.go

@@ -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

+ 8 - 0
memcache/memcache_test.go

@@ -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)