Skip to content
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
31 changes: 28 additions & 3 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (client *Client) openConnection() (c net.Conn, err error) {
if err != nil {
return
}

//handle authentication here authored by @shxsun
if client.Password != "" {
cmd := fmt.Sprintf("AUTH %s\r\n", client.Password)
Expand Down Expand Up @@ -472,8 +472,7 @@ func (client *Client) Flush(all bool) error {
// String-related commands

func (client *Client) Set(key string, val []byte) error {
_, err := client.sendCommand("SET", key, string(val))

_, err := client.sendCommand("SET", key, string(val))
if err != nil {
return err
}
Expand Down Expand Up @@ -1132,6 +1131,32 @@ func (client *Client) Hmset(key string, mapping interface{}) error {
return nil
}

func (client *Client) Hmget(
key string, fields ...string) (map[string]string, error) {

fs := make([]string,len(fields)+1)
fs[0] = key
copy(fs[1:],fields[:])
res, err := client.sendCommand("HMGET", fs...)
if err != nil {
return nil, err
}

data := res.([][]byte)
if data == nil || len(data) == 0 {
return nil,RedisError("Key `" + key + "` does not exist")
}

dl := len(data)
val := make(map[string]string)
for i,k := range fields {
if i < dl {
val[k] = string(data[i])
}
}
return val,err
}

func (client *Client) Hincrby(key string, field string, val int64) (int64, error) {
res, err := client.sendCommand("HINCRBY", key, field, strconv.FormatInt(val, 10))
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var client Client
func init() {
runtime.GOMAXPROCS(2)
client.Addr = "127.0.0.1:6379"
client.Addr = "192.168.18.18:6379"
client.Db = 13
}

Expand Down Expand Up @@ -646,6 +647,23 @@ func TestHash(t *testing.T) {
t.Fatal("verifyHash Hgetall failed")
}

test8,err := client.Hmget("h4","a","b","c","d","e")
if err != nil {
t.Fatal("verifyHash Hmget failed", err.Error())
}
test9 := map[string]string{
"a":"aaaaa",
"b":"bbbbb",
"c":"ccccc",
"d":"ddddd",
"e":"eeeee",
}

if !reflect.DeepEqual(test8, test9) {
t.Fatal("verifyHash Hget failed")
}


client.Del("h")
client.Del("h2")
client.Del("h3")
Expand Down Expand Up @@ -713,6 +731,14 @@ func BenchmarkHgetall(b *testing.B) {
client.Del("tjs")
}

func BenchmarkHmget(b *testing.B) {
client.Hmset("tjs", testObj)
for i := 0; i < b.N; i++ {
client.Hmget("tjs")
}
client.Del("tjs")
}

func BenchmarkJsonMget(b *testing.B) {
od, _ := json.Marshal(testObj)
client.Set("tjs", od)
Expand Down