-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add hash support #21
Comments
Redigo can execute any command including HMSET. Can you share the line of code where you invoke HMSET? |
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
"log"
)
var (
conn redis.Conn
)
// Connect to Redis
func init() {
var err error
conn, err = redis.Dial("tcp", ":6379")
if err != nil {
log.Fatalf("Couldn't connect to Redis: %v\n", err)
}
}
func main() {
defer conn.Close()
stockData := map[string]map[string]string{
"GOOG": {"company_name":"Google Inc.", "open_price":"803.99", "ask_price":"795.50", "close_price":"802.66", "bid_price":"793.36"},
"MSFT": {"ask_price":"N/A", "open_price":"28.30", "company_name":"Microsoft Corpora", "bid_price":"28.50", "close_price":"28.37"},
}
conn.Send("HMSET", "stocks")
// conn.Send("HMSET")
// conn.Send("stocks")
// var cmd []interface{}
for sym, row := range stockData {
for colName, val := range row {
key := sym + ":" + colName
// cmd = append(cmd, key, val)
conn.Send(key, val)
}
}
// conn.Send("HMSET", cmd...)
reply, err := conn.Do("EXEC")
if err != nil {
log.Fatalf("Error setting hash: %v\n", err)
}
fmt.Printf("reply == %+v\n", reply)
} |
The comments reveal the other versions I've also tried, such as sending (almost) all the commands at once with Any idea what I'm doing wrong? |
The following examples show how to create one hash for each symbol.
|
Here's another example that uses struct instead of map. This example stores the data in the same format as the previous examples.
|
Great examples, thanks! I read some more and it sounds like Redis doesn't support nested hashes, so I suppose what I was trying to do won't work: creating one Redis hash that contains another hash from symbols to stock data. I may try the probably-good-enough hack of storing some JSON as a string since that's really what I need. One way or the other, I'll be able to make this work, especially with the examples you've given. Thanks again! |
Nested hashes are not supported. The free and short The Little Redis Book gives a nice overview the types supported by Redis. It's common to store JSON in Redis. If you are only accessing the data from Go, then encoding/gob is another good option for storing nested data. |
I couldn't get the Go + redigo equivalent of
to work ("ERR wrong number of arguments for 'hmset' command"). Native hash support would be nice to get around this and provide convenience functions.
The text was updated successfully, but these errors were encountered: