Fetching Redis INFO using Redigo
Redigo is a Go client for the Redis database.
It may be useful to be able to fetch & use Redis Server information within your Go program. The library currently doesn’t offer an explicit method to query for the information returned by the Redis INFO command.
This post outlines how to query Redis INFO when using the redigo library in a GoLang project. As an example, we’ll be fetching the uptime_in_seconds
field from the INFO response.
First, ensure you have a redis connection to work with. In our example, this connection is named: conn
Send the "INFO"
command, and pass it to the String
helper method provided by redigo.
Next, we’ll use a regular expression to define the field we want to extract from the INFO response. In our case, uptime_in_seconds
field, which is: “Number of seconds since Redis server start”. Given that the value is expected to be an integer, our regex can explicitly search using a number.
And then, we’ll split the string using ":"
to get the uptime value
Finally, we convert the uptime value to an integer
Putting it all together, we can have a function that accepts the Redis connection, and return the uptime value:
This approach can be used to extract any field from the INFO result.
I’ll see if I can propose this as an addition to the library, since it could come in handy.
Happy Coding, and stay safe!