From ce9cfe9417774ec4e2a4e65dab39c261f239d6b6 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sun, 22 Jul 2018 09:46:29 +0300 Subject: [PATCH] Add MemoryUsage --- commands.go | 20 +++++++++++++++++--- commands_test.go | 31 ++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/commands.go b/commands.go index 7b6c65df..763c8370 100644 --- a/commands.go +++ b/commands.go @@ -62,6 +62,7 @@ type Cmdable interface { TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) TxPipeline() Pipeliner + Command() *CommandsInfoCmd ClientGetName() *StringCmd Echo(message interface{}) *StringCmd Ping() *StatusCmd @@ -275,9 +276,9 @@ type Cmdable interface { GeoRadiusByMemberRO(key, member string, query *GeoRadiusQuery) *GeoLocationCmd GeoDist(key string, member1, member2, unit string) *FloatCmd GeoHash(key string, members ...string) *StringSliceCmd - Command() *CommandsInfoCmd ReadOnly() *StatusCmd ReadWrite() *StatusCmd + MemoryUsage(key string, samples ...int) *IntCmd } type StatefulCmdable interface { @@ -355,6 +356,12 @@ func (c *statefulCmdable) SwapDB(index1, index2 int) *StatusCmd { //------------------------------------------------------------------------------ +func (c *cmdable) Command() *CommandsInfoCmd { + cmd := NewCommandsInfoCmd("command") + c.process(cmd) + return cmd +} + func (c *cmdable) Del(keys ...string) *IntCmd { args := make([]interface{}, 1+len(keys)) args[0] = "del" @@ -2301,8 +2308,15 @@ func (c *cmdable) GeoPos(key string, members ...string) *GeoPosCmd { //------------------------------------------------------------------------------ -func (c *cmdable) Command() *CommandsInfoCmd { - cmd := NewCommandsInfoCmd("command") +func (c *cmdable) MemoryUsage(key string, samples ...int) *IntCmd { + args := []interface{}{"memory", "usage", key} + if len(samples) > 0 { + if len(samples) != 1 { + panic("MemoryUsage expects single sample count") + } + args = append(args, "SAMPLES", samples[0]) + } + cmd := NewIntCmd(args...) c.process(cmd) return cmd } diff --git a/commands_test.go b/commands_test.go index 896aee90..6bec8116 100644 --- a/commands_test.go +++ b/commands_test.go @@ -244,14 +244,31 @@ var _ = Describe("Commands", func() { Describe("debugging", func() { It("should DebugObject", func() { - debug := client.DebugObject("foo") - Expect(debug.Err()).To(HaveOccurred()) - Expect(debug.Err().Error()).To(Equal("ERR no such key")) + err := client.DebugObject("foo").Err() + Expect(err).To(MatchError("ERR no such key")) - client.Set("foo", "bar", 0) - debug = client.DebugObject("foo") - Expect(debug.Err()).NotTo(HaveOccurred()) - Expect(debug.Val()).To(ContainSubstring(`serializedlength:4`)) + err = client.Set("foo", "bar", 0).Err() + Expect(err).NotTo(HaveOccurred()) + + s, err := client.DebugObject("foo").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(s).To(ContainSubstring("serializedlength:4")) + }) + + It("should MemoryUsage", func() { + err := client.MemoryUsage("foo").Err() + Expect(err).To(Equal(redis.Nil)) + + err = client.Set("foo", "bar", 0).Err() + Expect(err).NotTo(HaveOccurred()) + + n, err := client.MemoryUsage("foo").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(n).To(Equal(int64(52))) + + n, err = client.MemoryUsage("foo", 0).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(n).To(Equal(int64(52))) }) })