Browse Source

Added DeleteAll() method

Conflicts:
	memcache/memcache.go
	memcache/memcache_test.go
Kenny Grant 12 years ago
parent
commit
f52bf5c38e
2 changed files with 35 additions and 15 deletions
  1. 10 0
      memcache/memcache.go
  2. 25 15
      memcache/memcache_test.go

+ 10 - 0
memcache/memcache.go

@@ -99,6 +99,7 @@ func legalKey(key string) bool {
 var (
 	crlf            = []byte("\r\n")
 	space           = []byte(" ")
+	resultOK        = []byte("OK\r\n")
 	resultStored    = []byte("STORED\r\n")
 	resultNotStored = []byte("NOT_STORED\r\n")
 	resultExists    = []byte("EXISTS\r\n")
@@ -587,6 +588,8 @@ func writeExpectf(rw *bufio.ReadWriter, expect []byte, format string, args ...in
 		return err
 	}
 	switch {
+	case bytes.Equal(line, resultOK):
+		return nil
 	case bytes.Equal(line, expect):
 		return nil
 	case bytes.Equal(line, resultNotStored):
@@ -607,6 +610,13 @@ func (c *Client) Delete(key string) error {
 	})
 }
 
+// DeleteAll deletes all items in the cache.
+func (c *Client) DeleteAll() error {
+	return c.withKeyRw("", func(rw *bufio.ReadWriter) error {
+		return writeExpectf(rw, resultDeleted, "flush_all\r\n")
+	})
+}
+
 // Increment atomically increments key by delta. The return value is
 // the new value after being incremented or an error. If the value
 // didn't exist in memcached the error is ErrCacheMiss. The value in

+ 25 - 15
memcache/memcache_test.go

@@ -82,7 +82,7 @@ func testWithClient(t *testing.T, c *Client) {
 			t.Fatalf(format, args...)
 		}
 	}
-	mustSet := mustSetF(t, c) 
+	mustSet := mustSetF(t, c)
 
 	// Set
 	foo := &Item{Key: "foo", Value: []byte("fooval"), Flags: 123}
@@ -163,35 +163,45 @@ func testWithClient(t *testing.T, c *Client) {
 		t.Fatalf("increment non-number: want client error, got %v", err)
 	}
 	testTouchWithClient(t, c)
+
+	// Test Delete All
+	err = c.DeleteAll()
+	checkErr(err, "DeleteAll: %v", err)
+	it, err = c.Get("bar")
+	if err != ErrCacheMiss {
+		t.Errorf("post-DeleteAll want ErrCacheMiss, got %v", err)
+	}
+
 }
 
 func testTouchWithClient(t *testing.T, c *Client) {
 	if testing.Short() {
-		t.Skip("Skipping testing memcache Touch with testing in Short mode")
+		t.Log("Skipping testing memcache Touch with testing in Short mode")
+		return
 	}
-	
+
 	mustSet := mustSetF(t, c)
-	
+
 	const secondsToExpiry = int32(2)
-	
+
 	// We will set foo and bar to expire in 2 seconds, then we'll keep touching
 	// foo every second
 	// After 3 seconds, we expect foo to be available, and bar to be expired
-	foo := &Item{Key: "foo", Value: []byte("fooval"), Expiration: secondsToExpiry }
-	bar := &Item{Key: "bar", Value: []byte("barval"), Expiration: secondsToExpiry }
-	
+	foo := &Item{Key: "foo", Value: []byte("fooval"), Expiration: secondsToExpiry}
+	bar := &Item{Key: "bar", Value: []byte("barval"), Expiration: secondsToExpiry}
+
 	setTime := time.Now()
 	mustSet(foo)
 	mustSet(bar)
-	
-	for s:=0; s<3; s++ {
-		time.Sleep(time.Duration(1*time.Second))
+
+	for s := 0; s < 3; s++ {
+		time.Sleep(time.Duration(1 * time.Second))
 		err := c.Touch(foo.Key, secondsToExpiry)
-		if nil!=err {
+		if nil != err {
 			t.Errorf("error touching foo: %v", err.Error())
 		}
 	}
-	
+
 	_, err := c.Get("foo")
 	if err != nil {
 		if err == ErrCacheMiss {
@@ -200,9 +210,9 @@ func testTouchWithClient(t *testing.T, c *Client) {
 			t.Fatalf("unexpected error retrieving foo after touching: %v", err.Error())
 		}
 	}
-	
+
 	_, err = c.Get("bar")
-	if nil==err {
+	if nil == err {
 		t.Fatalf("item bar did not expire within %v seconds", time.Now().Sub(setTime).Seconds())
 	} else {
 		if err != ErrCacheMiss {