go-clients, can it persist structs?

6 views
Skip to first unread message

S Ahmed

unread,
Apr 23, 2016, 2:42:40 PM4/23/16
to redi...@googlegroups.com
Hi,

When saving go structs, can I just pass them into the go clients or do I have to convert them to a byte buffer first? i.e. I have to handle the ser and deserialization ?

Thanks!

The Real Bill

unread,
Apr 24, 2016, 11:46:56 AM4/24/16
to Redis DB
You will have to "convert" to the appropriate native Redisfata structure. If your structs are not nested, then you are probably best off using a hash where each struct member is a member of the hash with its value being the value.

For example if you have a struct called User with .Name, .Id, and .Email members, then (in libredis) you would call r.HSet(u.Id, "name", u.Name) to set the name. Alternatively there is HMSet where you could pass all members in a single call. Provided your structs aren't very large this would be more efficient.

You would change the second two arguments for each member. You would use r.HgetAll(Id) ( see docs: https://godoc.org/github.com/therealbill/libredis/client#Redis.HGetAll) to get a map[string]string response which you would the use to recreate/set your struct. If your structs are more complicated than that you may have to do some indexing and referencing.

As far as converting to byte arrays, libredis actually expects strings, though not all client libraries do. I'm mulling around adding calls which take []byte arrays but I'll have to validate they offer enough of a performance benefit for the added API bulk to justify them.

Cheers,
Bill

Reply all
Reply to author
Forward
0 new messages