Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats, stats items and stats slabs api added #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down