Using Radix in Golang: Creating & Testing a Redis Client




Using Radix in Golang: Creating & Testing a Redis Client   

I have been thrown into the world of Golang at my internship, and having never written in Go before, I struggled with the basics at first (and still struggle with different parts of Go). So, I thought it may be helpful to outline my process in creating a script that updates the TTLs (Time To Live) for cached keys in my work's Redis database.

Step One: Create a Redis Client 
I used the radix package from https://github.com/mediocregopher/radix. Radix gives me Redis functionality in Go. Additionally, I am creating logs using https://github.com/sirupsen/logrus to log any errors (since you can't return a value in func main()).

To create a Redis client, we need three things: connect a client to the Redis server via a TCP connection, you do this by connecting to port 6379, and the size of the pool. Here, we use radix's example size of 10. The pool size opens a number of connections, and this is relevant to us because we want to run concurrent goroutines, which will use the pools via radix's implicit pipelining. The Redis client returns two things: the connection pool and an error. If everything goes like you want it to, the error will be nil. In the unfortunate case that a connection can't be made, we should account for that. Therefore, if the error isn't nil, we will return the error. Otherwise, we keep going!

Step Two: Test Using Redis "GET" and "SET"
Radix enables Redis commands via client.Do(radix.Cmd("...cmds")). Great! In my code c is the client, so we call c.Do, and then we call radix.Cmd. We want to make a new key to test that all of this works. In order to do that, we need to "SET" a key, value pair. This inserts the key, value pair into our Redis database. The radix command in this instance uses (&setVal, "SET", key, value).  The radix command takes &setVal, a pointer to the variable setVal, the "SET" command telling Redis what to do, and a key and value which to set. The pointer setVal saves the return value of the set command. The Redis documentation does a great job of explaining each Redis command, and what should be returned. If "SET" works, we expect the value to be "OK".

For the "GET" command, there is a very similar structure. When using the redis-cli, we can type in "GET key" (with key being our real key, like keyone in the example below). So, we need one less parameter for "GET" than for "SET".  Use "GET" when you want to get the value of a key. Here is an example using the redis-cli to show how similar the two are.


The example below shows the creation of a Redis client, "SET" a key, value pair, and "GET" the value of a given key.


To get the results, we print the values from each command, and in our terminal we go to our directory containing our go file and `go run file.go`. No errors were logged, so we are good! Our output returns two values, OK and valueone (just like when using the redis-cli!). 


Practically speaking, we may not care to get an "OK" message, and we may not want to print it. If that is the case, we can replace "&setVal" in the command with "nil", and remove the "var setVal string" line. Now, we don't need to store the pointer value "OK". 


There you go! The very basics of using Radix in Go. Next, let's use Scan to scan all keys in the database. 

Comments

Popular Posts