Redis To Go on Heroku
Gem Setup
The redis-rb client is the easiest
way to access Redis from Ruby. Install redis-rb
locally with
sudo gem install redis
.
Using from Rails
For Rails 2.3.3 and higher, update the config/environment.rb
to include the
redis
gem.
config.gem 'redis'
If using Rails 3, update the Gemfile:
gem 'redis'
Setup the Redis connection information for development in
config/environments/development.rb
ENV["REDISTOGO_URL"] = 'redis://username:password@my.host:6789'
Configure Redis in config/initializers/redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Test that it works in the console.
$ rails console
>> REDIS.set("foo", "bar")
"OK"
>> REDIS.get("foo")
"bar"
Using from Sinatra
In the configure block include:
configure do
require 'redis'
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
Test that it works in the console.
$ irb -r myapp.rb
>> REDIS.set("foo", "bar")
"OK"
>> REDIS.get("foo")
"bar"
Deploying to Heroku
To use Redis To Go on Heroku, install the redistogo add-on:
$ heroku addons:add redistogo
Test that it works from the Heroku console:
$ heroku console
>> REDIS.set("answer", 42)
"OK"
>> REDIS.get("answer")
42