That’s because Heroku’s ever-growing list of Redis providers all use config vars to set the Redis URL and redis-rails expects you to hook up the cache store and session store during the Rails application boot process, during which the config vars are unavailable.
Here’s how I got it working:
config/initializers/session_store.rb. It’s not needed because we’ll initialize the session store and the cache store in the same place.
config/initializers/redis_store.rb and put this in it:
redis_url = ENV["REDISCLOUD_URL"] || "redis://127.0.0.1:6379/0/myapp" MyApp::Application.config.cache_store = :redis_store, redis_url MyApp::Application.config.session_store :redis_store, redis_server: redis_url
That’s all there is to it! A few notes:
- initializers are executed after
application.rb, at which point Heroku’s config vars are available
- when the config var URL is nil, like in dev mode, it will revert to your local Redis server
- I’m using RedisCloud as a Redis provider, but you’ll need to adjust the config var to match whichever provider you choose
- Be sure to replace
MyAppwith the name of your app ;)
/myappat the end of the fallback URL is optional. It adds a namespace to all Redis keys. I like this in dev mode because I can see all of my app’s keys by doing a
keys myapp*in the Redis CLI. Feel free to omit