Skip to content

Instantly share code, notes, and snippets.

@pubis
Created December 11, 2011 09:07
Show Gist options
  • Save pubis/1459506 to your computer and use it in GitHub Desktop.
Save pubis/1459506 to your computer and use it in GitHub Desktop.
Redis config and initialization for rails
#config/initializers/redis.rb
require 'redis'
require 'redis/objects'
REDIS_CONFIG = YAML.load( File.open( Rails.root.join("config/redis.yml") ) ).symbolize_keys
dflt = REDIS_CONFIG[:default].symbolize_keys
cnfg = dflt.merge(REDIS_CONFIG[Rails.env.to_sym].symbolize_keys) if REDIS_CONFIG[Rails.env.to_sym]
$redis = Redis.new(cnfg)
Redis::Objects.redis = $redis
#$redis_ns = Redis::Namespace.new(cnfg[:namespace], :redis => $redis) if cnfg[:namespace]
# To clear out the db before each test
$redis.flushdb if Rails.env = "test"
#config/redis.yml
default:
host: localhost
port: 6379
development:
db: 0
# namespace: appname_dev
test:
db: 1
# namespace: appname_test
production:
db: 2
host: 192.168.1.100
# namespace: appname_prod
@abla00
Copy link

abla00 commented Feb 23, 2018

This is really helpful and here is a reminder.
I got the error when running RSpec:

There was an error while trying to load the gem 'redis'.
Gem Load Error is: uninitialized constant Redis

I fix it by renaming lib/redis.rb to other name, ex: lib/foo.rb
Maybe the file name redis.rb is confused with the gem lib redis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment