diff --git a/memcache/memcache.go b/memcache/memcache.go index b98a7653..61b23a89 100644 --- a/memcache/memcache.go +++ b/memcache/memcache.go @@ -324,6 +324,62 @@ func (c *Client) Get(key string) (item *Item, err error) { return } + +// Get Stats as an array of String +func (c *Client) Stats() ([]string, error) { + addr, err := c.selector.PickServer("tmp") + if err != nil { + return nil,err + } + return c.getStatsFromAddr(addr,"stats\r\n") +} + +func (c *Client) ItemStats() ([]string, error) { + addr, err := c.selector.PickServer("tmp") + if err != nil { + return nil,err + } + return c.getStatsFromAddr(addr,"stats items\r\n") +} + +func (c *Client) SlabsStats() ([]string, error) { + addr, err := c.selector.PickServer("tmp") + if err != nil { + return nil,err + } + return c.getStatsFromAddr(addr,"stats slabs\r\n") +} + +func (c *Client) getStatsFromAddr(addr net.Addr,command string) ([]string,error) { + cn, err := c.getConn(addr) + if err != nil { + return nil,err + } + defer cn.condRelease(&err) + + if _, err := fmt.Fprintf(cn.rw, command); err != nil { + return nil,err + } + if err := cn.rw.Flush(); err != nil { + return nil,err + } + return parseStatsResponse(cn.rw.Reader) +} + +func parseStatsResponse(r *bufio.Reader) ([]string,error) { + stats := []string{} + for { + line, err := r.ReadSlice('\n') + if err != nil { + return stats,err + } + if bytes.Equal(line, resultEnd) { + return stats,nil + } + stats = append(stats,string(line)) + } +} + // Touch updates the expiry for the given key. The seconds parameter is either // a Unix timestamp or, if seconds is less than 1 month, the number of seconds // into the future at which time the item will expire. ErrCacheMiss is returned if the @@ -334,6 +390,8 @@ func (c *Client) Touch(key string, seconds int32) (err error) { }) } + + func (c *Client) withKeyAddr(key string, fn func(net.Addr) error) (err error) { if !legalKey(key) { return ErrMalformedKey diff --git a/memcache/memcache_test.go b/memcache/memcache_test.go index 4b52a911..7a2dee98 100644 --- a/memcache/memcache_test.go +++ b/memcache/memcache_test.go @@ -79,6 +79,14 @@ func mustSetF(t *testing.T, c *Client) func(*Item) { } } +func TestStats(t *testing.T) { + client := New(testServer) + _, err := client.Stats() + if err != nil { + t.Errorf("error fetching stats %v", err) + } +} + func testWithClient(t *testing.T, c *Client) { checkErr := func(err error, format string, args ...interface{}) { if err != nil {