Redis

Access Redis Via Ruby

Redis is a free and open-source in-memory key-value data store famous for high performance, low latency, and flexibility.

This tutorial will help you to interact with a Redis database using the Ruby programming language.

Step 1 – Install Redis Server

The first step is installing and setting up a Redis cluster on our local system. In this guide, we are using an Ubuntu 20.04 system. However, the commands should work on any Debian-based system.

Start by updating the system repository cache.

sudo apt-get update

Next, Install Redis using the command:

sudo apt-get install redis-server -y

Once the installation is complete, run the following command to verify the Redis server is installed.

redis-server --version

You should see an output as shown:

Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=636cde3b5c7a3923

Step 2 – Start Redis and Connect to the CLI

The next step is to start the Redis server. Using systemctl, enter the command:

sudo service redis-server start

The above command should start the Redis cluster and connect to the database.

Use the command below to connect to Redis using the CLI.

$ redis-cli
127.0.0.1:6379>

Once connected, you can begin to use Redis.

Step 3 – Installing Ruby

Ruby is a powerful programming language with tons of features for any modern developer. It offers an incredible syntax that is easy to read and understand. It is, therefore, an appropriate choice for interacting with Redis.

To use Ruby, we need to install it.

The simplest method to set up Ruby on our system is to use the APT package managers.

Start by updating the system repo cache.

sudo apt-get update

Next, install Ruby with the command:

sudo apt-get install ruby-full

Once the command is complete, use the command below to verify Ruby is installed.

ruby --version

If installed, you should get an output as shown:

ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]

Step 4 – Using Ruby to Interact with Redis

After we have our development environment set up, we can use Ruby with Redis.

We need a Ruby gem developed to interact with Redis to do this. In this tutorial, we have chosen the redis-rb gem.

Use the gem command to install it.

sudo gem install redis
Fetching redis-4.5.1.gem
Successfully installed redis-4.5.1
Parsing documentation for redis-4.5.1
Installing ri documentation for redis-4.5.1
Done installing documentation for redis after 0 seconds
1 gem installed

Once the gem is installed, we can start using it to interact with Redis.

Step 5 – Connect to Redis

Start by creating a ruby file.

touch ruby-redis.rb

Edit the file with your favorite text editor.

vim ruby-redis.rb

Add the code below to connect to your Redis cluster.

require "redis"
redis = Redis.new(host: "127.0.0.1", port: 6379, db: 0)

The code above imports the Redis package and creates a new connection. If you are using the default Redis configuration, you can omit the host, port, and db options.

Step 6 – Using Redis database with Ruby

To create a new key-value pair to a Redis database using Ruby, we can use the code as shown:

require "redis"
redis = Redis.new
redis.set("mykey", "myvalue")
redis.get("mykey")

The above code creates a new key and value using the set command. To fetch the value stored in a specific key, use the get method and pass the target key as the argument.

HINT: The redis-rb gem uses native Ruby commands to perform the operations.

Example 1

The following code uses Ruby to create a sorted set in Redis.

require "redis"
redis = Redis.new
redis.zadd("databases" 1, "MySQL")
redis.zadd("databases" 10, "FaunaDB")
redis.zadd("databases" 3, "Firestore")
redis.zadd("databases" 2, "MongoDB")
redis.zadd("databases" 5, "SQLite")

To get the elements in the sorted set, use the ZRANGE method.

redis.zrange("databases", 0, 10)

Example 2

You can add lists to a Redis database using Ruby as:

require "redis"
redis = Redis.new
redis.lpush("langs", "Python")

To get the items in a list, use the LRANGE method as:

redis.LRANGE("langs", 0, 10)

Closing

This guide demonstrates how to interact with a Redis database using the Ruby programming language. Check the redis-rb documentation to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list