Redis Basics

#56 Redis Basics
11/28/2016

Summary

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.
2
rails cache performance

Resources

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

Summary

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
  }
secrets.ymldevelopment:
  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 %>


Photo
Jonas Lin said 9 days ago:

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

`post' => 'port'

635114?v=3&s=64
kobaltz said 9 days ago:

Thanks! Show Notes have been corrected.

142157?v=3&s=64
dancinglightning said 7 days 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 ?

thx


635114?v=3&s=64
kobaltz said 7 days 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).

00000000000000000000000000000000?d=mm&f=y&s=64
adsr13 said 6 days 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 127.0.0.1:3000 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! 

00000000000000000000000000000000?d=mm&f=y&s=64
adsr13 said 6 days 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?

635114?v=3&s=64
kobaltz said 6 days 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