Created
December 11, 2011 09:07
-
-
Save pubis/1459506 to your computer and use it in GitHub Desktop.
Redis config and initialization for rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
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
Modified @coconup's example:
Usage: