# MacOS Terminal
brew install redis
brew services start redis
# Ubuntu Terminal
sudo apt install redis-server
# irb
2.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"
Using Redis as a cache
# Gemfile
gem '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.yml
development:
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 %>