Skip to content

Commit

Permalink
Add the MGET command
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfowler committed Apr 7, 2016
1 parent 5fb209a commit 3b4e4d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions wredis_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ func (w *Wredis) Get(key string) (string, error) {
return w.ExecString(get)
}

// MGet returns the values of all provided keys. For a key that does not exist,
// an empty string is returned. See http://redis.io/commands/mget.
func (w *Wredis) MGet(keys []string) ([]string, error) {
for _, key := range keys {
if key == "" {
return stringsError("keys cannot be empty")
}
}
return w.ExecStrings(func(conn redis.Conn) ([]string, error) {
args := redis.Args{}.AddFlat(keys)
return redis.Strings(conn.Do("MGET", args...))
})
}

// Incr increments the number stored at key by one.
// See http://redis.io/commands/incr
func (w *Wredis) Incr(key string) (int64, error) {
Expand Down
21 changes: 21 additions & 0 deletions wredis_strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ var _ = Describe("Strings", func() {
Ω(val).Should(Equal(testVal))
})

Context("MGET", func() {
It("should return an error when a key is empty", func() {
_, err := safe.MGet([]string{"1", "2", ""})
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("keys cannot be empty"))
})

It("should return all values for the provided keys", func() {
// insert keys into redis
Ω(safe.Set("1", "one")).Should(Succeed())
Ω(safe.Set("2", "two")).Should(Succeed())
// get values
vals, err := safe.MGet([]string{"1", "2", "3"})
Ω(err).ShouldNot(HaveOccurred())
Ω(vals).Should(HaveLen(3))
Ω(vals[0]).Should(Equal("one"))
Ω(vals[1]).Should(Equal("two"))
Ω(vals[2]).Should(Equal(""))
})
})

Context("INCR", func() {

It("should return an error with an empty key provided", func() {
Expand Down

0 comments on commit 3b4e4d4

Please sign in to comment.