Find tutorials, examples and technical articles that will help you to develop with Redis and Ruby.
In order to use Redis with Ruby you will need a Ruby Redis client. In the following sections, we will demonstrate the use of redis-rb, a Ruby client library for Redis. Additional Ruby clients for Redis can be found under the Ruby section of the Redis Clients page.
Redis is an open source, in-memory, key-value data store most commonly used as a primary database, cache, message broker, and queue. Redis cache delivers sub-millisecond response times, enabling fast and powerful real-time applications in industries such as gaming, fintech, ad-tech, social media, healthcare, and IoT.
Step 1. Run a Redis server
You can either run Redis in a Docker container or directly on your machine. Follow the below commands to setup a Redis server on Mac OS:
brew tap redis-stack/redis-stack
brew install --cask redis-stack
Redis Stack unifies and simplifies the developer experience of the leading Redis modules and the capabilities they provide. Redis Stack bundles five Redis modules: RedisJSON, RedisSearch, RedisGraph, RedisTimeSeries, and RedisBloom. Learn more
Ensure that you are able to use the following Redis command to connect to the Redis instance.
Now you should be able to perform CRUD operations with Redis commands. For example, you can insert data to Redis with the
SET command and the fetch it with the
GET command. The above Redis client command might require a password if you have setup authentication in your Redis configuration file. By default, Redis listens on port 6379. This can be modified in the Redis configuration file.
Step 2. Clone the repository
git clone https://github.com/redis-developer/redis-ruby-getting-started
Step 3. Install redis-rb:
$ gem install redis
Step 4. Use Gemfile
Step 5. Execute
$ bundle install
Using bundler 2.2.6
Using redis 4.2.5
Following files may not be writable, so sudo is needed:
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Step 6. Verifying
bundle info redis
* redis (4.2.5)
Summary: A Ruby client library for Redis
Source Code: https://github.com/redis/redis-rb/tree/v4.2.5
Bug Tracker: https://github.com/redis/redis-rb/issues
Step 7. Opening a Connection to Redis Using redis-rb
The following code creates a connection to Redis using redis-rb:
redis = Redis.new(host: "localhost", port: 6379, db: 11)
redis.set("mykey", "hello world")
To adapt this example to your code, make sure that you replace the following values with those of your database:
You can find connect.rb under this directory which you can directly use to test it.
Just execute the below command:
Step 8. Verifying
1614684665.728109 [0 [::1]:50918] "select" "11"
1614684665.728294 [11 [::1]:50918] "set" "mykey" "hello world"
1614684665.728435 [11 [::1]:50918] "get" "mykey"
Redis Launchpad is like an “App Store” for Redis sample apps. You can easily find apps for your preferred frameworks and languages. Check out a few of these apps below, or click here to access the complete list.