Redis on Rails

Quite often I see users asking on #rubyonrails how to setup a persistent connection with redis + rails. It's actually quite simple and you can go about it several ways.  A common approach I see is to use the global variable $redis throughout your Rails application. If you're on Rails 3.0 the config/initializers directory is autoloaded on boot.

# config/redis.yml

host: localhost

port: 6379


# config/initializers/redis.rb

config = YAML.load_file(Rails.root + 'config/redis.yml')


$redis = => config["host"], :port => config["port"])

Go Back