Updated February 17, 2023
Introduction to Redis gem
Redis gem is used to use the ruby with redis, at the time of using redis with the ruby we require a redis client of ruby. At the time of installing redis locally we are installing the same by using the apt command on ubuntu, but suppose we want to install redis with ruby then same time we are using gem and redis. We are installing the redis server by using gem.
Key Takeaways
- To interact with the ruby, we are using gem in redis. We are installing the redis by using gem for interacting the redis database with ruby.
- We can use the redis gem on the docker container as well as on our system, by installing redis using gem.
What is Redis Gem?
As we know that we can directly interact with redis by using redis cli, but if suppose we want to work with redis by using ruby then we need to use gems.
Following are the gems available in redis as follows:
- Oxblood
- redic
- redis-rb
Above are the gems available in redis, but the officially recommended gems for developers is redis-rb. By installing redis with gems we can connect to the server of redis and also start working by using the same. As we know that redis is an open-source and in-memory key-value data store that was commonly used in the primary database, queue, and message broker. Redis cache will deliver the sub milliseconds of response time which was enabling powerful and fast applications which are running in real-time.
We can run the redis on the docker container or we can also run the same on our machine. We are using the redis database in the cache system which was used in a server group by using the replication of primary and secondary.
How to Use Redis gem?
For using it we need to install the package of gem in our system first.
1. First, we are installing the gem in our system. We are installing gem by using the apt command as follows.
Command:
# apt install gem
Output:
2. After installing the gem now in this step, we are installing the redis by using the gem. We can see that we are installing the redis version as 5.0.5. While installing this document in the below snap, we can see that it will parse the documentation.
Command:
# gem install redis
Output:
3. After installing the redis by using gems now in this step we are using the below code for connecting to the redis.
Code:
require 'redis'
redis = Redis.new (
:host => ‘ ’,
:port => 6379,
:password => 'Pass@123')
Output:
4. After creating the connection code now in the below example, we are adding the redis set and get the command as follows. In the below example, we are creating the example.rb file.
Command:
require 'redis'
redis = Redis.new()
redis.set("redis_gem", "redis gem")
redis.get("redis_gem")
Output:
5. While creating the example.rb file now in this step we are running this file using the ruby command.
Command:
# ruby example.rb
Output:
6. After executing the file by using the ruby command we can check the execution by using the monitor command as follows.
Command:
# redis-cli
MONITOR
Output:
Redis gem Versions
There is multiple version available for redis gem. Currently, the latest and most stable version is 5.0.5. We can check the redis gem version by using the following command. We can see that we have installed the latest version of redis gem i.e. 5.0.5.
Command:
gem list | grep redis
Output:
The version contains three parts i.e. major release, minor release, and patch release. We have the 5.0.5 version installed that first 5 show the major release, the middle zero is showing the minor release and the last 5 is showing the patch release.
Below is the redis gem version as follows:
- 0.1: This version is released on 10th June 2009. After this release, another minor version and patches will release like 0.1, 0.1.1, 0.1.2, and 0.2.0.
- 0.0: This version is released on 2nd April 2010. After this release, another minor version and patches will release like 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6, and 1.0.7.
- 0.0: This version is released on 14th May 2010. After this release, another minor version and patches will release like 2.0.1, 2.0.2, 2.0.3, 2.0.4, 2.0.5, 2.0.6, 2.0.7, 2.0.8, 2.0.9, 2.0.10, 2.0.11, 2.0.12, 2.0.13, 2.1.0, 2.1.1, 2.2.0, 2.2.1 and 2.2.2.
- 0.0: This version is released on 23rd May 2012. After this release, another minor version and patches will release like 3.0.1, 3.0.2, 3.0.3, 3.0.4, 3.0.5, 3.0.6, 3.0.7, 3.1.0, 3.2.0, 3.2.1, 3.2.2, 3.3.0, 3.3.1, 3.3.2, 3.3.3, 3.3.4 and 3.3.5.
- 0.0: This version is released on 25th August 2017. After this release, another minor version and patches will release like 4.0.1, 4.0.2, 4.0.3, 4.1.0, 4.1.1, 4.1.2, 4.1.3, 4.1.4, 4.2.0, 4.2.1, 3.2.2, 4.2.3, 4.2.4, 4.2.5, 4.3.0, 4.3.1, 4.4.0, 4.5.0, 4.5.1, 4.6.0, 4.7.0, 4.7.1 and 4.8.0.
- 0.0: This version is released on 29th August 2022. After this release, another minor version and patches will release like 5.0.1, 5.0.2, 5.0.3, 5.0.4, and 5.0.5.
Examples of Redis gem
Given below are the examples mentioned:
Example #1
In the below example, we are connecting to the redis database and using the set and get command for creating the redis-rb file.
Command:
require 'redis'
redis = Redis.new()
redis.set("redis_gem_example", "redis gem_example")
redis.get("redis_gem_example")
Output:
Example #2
In the below example, we are connecting to the redis database and using the set command for creating the redis-rb file.
Command:
require 'redis'
redis = Redis.new()
redis.set("redis_gem_example", "redis gem_example")
Output:
Example #3
In the below example, we are connecting to the redis database and using the hset command for creating the redis-rb file as follows.
Command:
require 'redis'
redis = Redis.new()
redis.hset("redis_gem_example", ”field1”, "redis gem_example")
Output:
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of redis gem?
Answer: It is basically used to use the ruby with the redis database. Without installing gem with redis we cannot use ruby in the redis database.
Q2. How many types of gems are available in redis?
Answer: In redis basically there are three types of gem available i.e. oxblood, redis, and redis-rb.
Q3. How can we check the execution of redis-rb file?
Answer: We are executing the redis-rb file by using the ruby and filename commands and we are checking the execution by using the monitor command.
Conclusion
It is used to use the ruby with redis, at the time of using redis with the ruby we required a redis client of ruby. The officially recommended gems for developers is redis-rb. By installing redis with gems we can connect to the server of redis and also start working by using the same.
Recommended Articles
This is a guide to Redis gem. Here we discuss the introduction, how to use redis gem? versions and examples respectively. You may also have a look at the following articles to learn more –