#56 Redis Basics


Redis within a Ruby on Rails application has many benefits. Learn to interact with Redis Server and set it up for caching within your application.
rails cache performance 5:42 min


redis-rb gem - https://github.com/redis/redis-rb
redis-rails gem - https://github.com/redis-store/redis-rails
Redis Server - https://redis.io/


MacOS Terminalbrew install redis
brew services start redis
Ubuntu Terminalsudo apt install redis-server
irb2.3.3 :001 > require 'redis'
 => true

2.3.3 :002 > redis = Redis.new
 => #

2.3.3 :003 > redis.set 'key', 'value'
 => "OK"

2.3.3 :004 > redis.get 'key'
 => "value"

2.3.3 :005 >

Using Redis as a cache

Gemfilegem 'redis-rails'
config/environments/development.rb  config.cache_store = :redis_store, {
    host: 'localhost',
    port: 6379,
    db: 0,
    namespace: '056redis'

You will also need to configure the production environment file with the Redis cache. Instead of specifying the values within each configuration file, I recommend using the secrets.yml file instead. For example, 

config/environments/development.rb  config.cache_store = :redis_store, {
    host: Rails.application.secrets.redis_server,
    port: Rails.application.secrets.redis_port,
    db: Rails.application.secrets.redis_cache_database,
    namespace: Rails.application.secrets.redis_cache_namespace
  secret_key_base: ...
  redis_server: localhost
  redis_port: 6379
  redis_cache_database: 0
  redis_cache_namespace: '056redis'
index.html<% cache 'word-of-the-hour', expires_in: Time.now.beginning_of_hour + 1.hour do %>
  <%= %w(happy developer watch drifting ruby screencasts about rails).sample %>
<% end %>

Jonas Lin said 12 months ago:

There is a little bit spelling mistake in config/environments/development.rb:

`post' => 'port'

kobaltz PRO said 12 months ago:

Thanks! Show Notes have been corrected.

dancinglightning said 12 months ago:

Nice. Maybe next some on how to cache (an expire) actual models with association.

But my question: Was there something about the ubuntu install ? Specifically how to autostart that?

And for a shared host (many apps, many mysql's ) , are different db or namespaces or redis instances better ?


kobaltz PRO said 12 months ago:

I'll usually use the same database but different namespaces. However, I will put services on their own database. For example, if I were using Redis for caching as well as storing the User Sessions, I would put caching on db 0 and the sessions on db 1. This makes it easier to flush all of the stored cache without wiping out the user sessions (or vice versa).

adsr13 said 12 months ago:

I'm using postgresql installed by brew. which I believe is running on default port 5234. Your vid explains I can set a new ip in my case where my rails app run. My questions is how do I make my rails app, postgressql db, and redis server all sing together? Thank you! 

adsr13 said 12 months ago:

After creating a visitors controller for my example app to go along with this vid. I got this error

cdb:redis adsr13$ bin/rails rails g controller visitors index

rails aborted!

TypeError: Redis is not a module

/Users/adsr13/Google Drive/workspace/redis/config/application.rb:9:in `<top (required)>'

/Users/adsr13/Google Drive/workspace/redis/Rakefile:4:in `require_relative'

/Users/adsr13/Google Drive/workspace/redis/Rakefile:4:in `<top (required)>'

Please advise?

kobaltz PRO said 12 months ago:

It looks like you may have created a Rails app called Redis. This could be causing a conflict with the gem. You should think of Redis as a reserved name.

Login to Comment