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
@MatthiasWinkelmann
Copy link

Warning, Will Robinson!

https://gist.github.com/pubis/1459506#file-redis-rb:14 has the classic '=' vs. '==' bug.

If you're wondering why your Redis FB is reset every time you restart, the line

$redis.flushdb if Rails.env = "test"

should obviously be

$redis.flushdb if Rails.env == "test"

@jonopray
Copy link

cnfg = dflt.merge(REDIS_CONFIG[Rails.env.to_sym].symbolize_keys) if REDIS_CONFIG[Rails.env.to_sym] will be set to nil rather than the default if Rails.env is not a key in REDIS_CONFIG.

@coconup
Copy link

coconup commented Jul 27, 2017

Something easier and more up to date:

config/redis.yml

default: &default
  url: redis://localhost:6379
  db: 1

development:
  <<: *default
test:
  <<: *default
production:
  <<: *default
  url: redis://somehost:6379

lib/redis_cache.rb

class RedisCache
  def self.new
    Redis.new(Rails.application.config_for(:redis))
  end
end

config/initializers/redis_cache.rb

module MyApp
  class Application < Rails::Application
    config.autoload_paths += Dir[File.join(Rails.root, "lib", "redis_cache.rb")].each {|l| require l }
  end
end

Usage:

RedisCache.new.set("some_key", "some_value")

@onesneakymofo
Copy link

onesneakymofo commented Sep 22, 2017

Modified @coconup's example:

# config/redis.yml

default: &default
  url: redis://localhost:6379
  db: 0

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default
  url: redis://somehost:6379
# lib/redis.rb

$redis = Redis.new(Rails.application.config_for(:redis))
# config/initializers/redis.rb
# You can probably move this to the autoload_paths in your regular config/application.rb

Rails.application.config.autoload_paths += Dir[File.join(Rails.root, "lib", "redis.rb")].each {|l| require l }

Usage:

$ $redis.set("foo", "bar")
> "OK"
$ $redis.get("foo")
> "bar"

@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