Skip to content

Commit

Permalink
Setnx, Msetnx: return bool indicating success
Browse files Browse the repository at this point in the history
  • Loading branch information
cthom06 authored and root committed Jul 1, 2010
1 parent 3bb600e commit 0680ed5
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,16 @@ func (client *Client) Mget(keys []string) ([][]byte, os.Error) {
return data, nil
}

func (client *Client) Setnx(key string, val []byte) os.Error {
_, err := client.sendCommand("SETNX", []string{key, string(val)})
func (client *Client) Setnx(key string, val []byte) (bool, os.Error) {
res, err := client.sendCommand("SETNX", []string{key, string(val)})

if err != nil {
return err
return false, err
}

return nil
if data, ok := res.(int64); ok {
return data == 1, nil
}
return false, RedisError("Unexpected reply to SETNX")
}

func (client *Client) Setex(key string, time int64, val []byte) os.Error {
Expand Down Expand Up @@ -426,19 +428,22 @@ func (client *Client) Mset(mapping map[string][]byte) os.Error {
return nil
}

func (client *Client) Msetnx(mapping map[string][]byte) os.Error {
func (client *Client) Msetnx(mapping map[string][]byte) (bool, os.Error) {
args := make([]string, len(mapping)*2)
i := 0
for k, v := range mapping {
args[i] = k
args[i+1] = string(v)
i += 2
}
_, err := client.sendCommand("MSETNX", args)
res, err := client.sendCommand("MSETNX", args)
if err != nil {
return err
return false, err
}
return nil
if data, ok := res.(int64); ok {
return data == 0, nil
}
return false, RedisError("Unexpected reply to MSETNX")
}

func (client *Client) Incr(key string) (int64, os.Error) {
Expand Down

0 comments on commit 0680ed5

Please sign in to comment.