Reading and Writing Redis Protocol in Go
redisgreen.net
redisgreen.net
This is a mature library that I have been using for years.
value, err := redis.String(client.Do("GET", "mykey"))
Of course, this means you need to know about every command's response type and adds an extra (if small) level of verbosity, but in practice it fits very well with Go's philosophy of handling errors often and early. So, for instance, you're encouraged to handle the possibility that your key doesn't exist rather than barreling ahead with an empty string.How is this better for error handling? Why couldn't client.Get("mykey") signal an error in the same way, rather than returning an empty string?
value, err := redis.Integer(client.Do("GET", "mykey"))
sets the int variable "value" to the decimal integer stored in "mykey". The variable err is set to a non-nil value if the value cannot be parsed as a decimal integer, the key is missing, the key is not a redis string, the connection is broken or any other error.This method of error handling is convenient because the application only needs to check for one error. The alternative is to do something like:
v, err := client.Do("GET", "mykey")
if err != nil {
// handle command error
}
p, ok := v.(string)
if !ok {
// handle error where p is not a redis string
}
i, err := strconv.ParseInt(string(p))
if err != nil {
// handle parse error
}
There is no trap. If the connection to the server is healthy and the value stored for "mykey" is a decimal encoded integer, then redis.Integer(client.Do("GET", "mykey")) returns that integer.http://en.wikipedia.org/wiki/Chunked_transfer_encoding
And why did they choose these prefix chars ($, *, +, -) ?
Either way, I'm glad there's some work on another, although this looks like it's essentially a RESP parser rather than aiming to ever be a Redis interface.
Besides, reading RESP objects is only the very first step in implementing a true Redis client. You need to handle response types (null bulk strings, null arrays, etc), opening TCP connections and handling errors therein, and offer up a sane API for end-users. And that's definitely outside the scope of the blog post. :)
Products like RedisGreen host their servers within Amazon and Google's data centers, and are meant for customers hosting their apps within those data centers. In the case of RedisGreen, you choose your hosting provider, region, etc, when provisioning resources.
If you sent database queries across the open Internet, ping latency is just one of several problems you'd encounter.
(disclosure: I work for RedisGreen)