Our Rails App and Redis Communication
In this lesson, we will explore the connection between Rails and Redis. We will also make some amendments to view webpages.
Our Rails app talking to Redis
Although it is great that we have started up a Redis server using Compose, it is not much use to us by itself. The whole point of running the Redis server is so our Rails app can talk to it and use it as a key-value store. So let’s connect our Rails app to Redis and actually use it for something.
There are a million ways an app might want to use Redis. For our purposes, though, we do not really care what we use Redis for: we care more about how we use it. We are intentionally going to use a basic example. Our Rails app will simply store and retrieve a value. However, keep the larger point in mind: once you know how to set up the Rails app to talk to the Redis server in a container, you can use it however you like. Ready? Let’s begin.
Installing the Redis gem
The first thing we need to do to get our Rails app talking to Redis is to install the redis
gem. You may remember that to update our gems, we need to update our image.
So first, in our Gemfile, the redis
gem needs to be uncommented:
gem 'redis', '~> 4.0'
...