Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Redis::Objects.redis is not working #39

Closed
cardinalblue opened this Issue Aug 9, 2011 · 8 comments

Comments

Projects
None yet
6 participants
@ghost

ghost commented Aug 9, 2011

It is shown in the documentation that the initialization can be done by this:

Redis::Objects.redis = Redis.new(:host => '127.0.0.1', :port => 6379)

However, it is not working. Actually, it should be like this:

Redis.current = Redis.new(:host => '127.0.0.1', :port => 6379)

Is this a documentation error? Or am I getting something wrong?

Owner

nateware commented Aug 27, 2011

Can you copy-and-paste all your code, including the "require" lines?

The usage you cite has test coverage so I'm guessing it might be a weird conflict.

I'm seeing the same issue in a Rails 3.1 application on 1.9.3-p0 (initializer snippet below). I'm using Redis::Namespace to keep all my different redis "scopes" isolated within the same project (resque/redis-objects/hand rolled stuff). The redis objects config below don't work, exactly like the original reporter noted. I'm looking into this a bit more, will post more comments if and when I discover things.

redis_config = YAML.load_file Rails.root.join( 'config/redis.yml' )
host, port = redis_config[ Rails.env ].split(':')

# Setup the connections as a constant
REDIS = Redis.new( :host => host, :port => port )

# Snipped passenger smart spawning code...

# Setup redis-objects using a unique namespace
ns = "app:objects:#{Rails.env}"

require 'redis/objects'
Redis::Objects.redis = Redis::Namespace.new( ns, :redis => REDIS )

Using the above, Redis::Objects always connects to localhost:6379. Resque and everything else works as expected.

Man, this is just so wrong... The confusion comes from the dual use of Redis::Objects.redis and $redis. I got the exception from using Redis::Lock, assuming it would simply use Redis::Objects.redis...

IMHO, Redis::BaseObject#initialize should check Redis::Objects.redis instead of $redis as a fall back. This dual configuration is confusing, and now with using Redis::Namespace I'll need to remember to explicitly pass a redis connection object in args to all the BaseObject sub-classes...

I think this is a documentation bug and should be made more clear in the beginning of the documentation, or at least combined under a single configuration heading. Going over the README (after reading the code), it is now as clear as daylight to me, but at first glance it wasn't.

Owner

nateware commented Feb 22, 2012

Kenneth, I updated the README to refer to Redis.current rather than $redis. Unfortunately, Redis::Objects.redis doesn't actually work everywhere due to chicken-egg loading issues.

@nateware nateware closed this Feb 22, 2012

stas commented Feb 22, 2012

Off topic. Just my 2 cents...
I would suggest stop using namespaces and switch to a separate clean db based on your environment.

ENV["REDIS_URL"] = 'redis://127.0.0.1:6379/111'

I think the benefits are pretty obvious in this case.

Owner

nateware commented Feb 22, 2012

Excellent point. For Rails, I use this snippet in config/initializers/redis.rb that works with Heroku or any other app that sets REDISTOGO_URL:

if url = ENV['REDISTOGO_URL']
  uri = URI.parse(url)
  Redis.current = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
elsif Rails.env.development? or Rails.env.test?  # or check ENV['RACK_ENV']
  Redis.current = Redis.new  # localhost
else
  # supposed to have a server
  raise "Missing redis server - please set REDISTOGO_URL env var"
end

oogali commented Feb 23, 2012

Here's what I use in my Sinatra initialization:

# establish redis connection
redis = URI.parse(ENV['REDIS_URL'] || 'redis://127.0.0.1:6379')
Redis::Objects.redis = Redis.new(
  :host => redis.host,
  :port => redis.port || 6379,
  :password => redis.password,
  :db => redis.path
)

Hi, I am trying to deploy to Heroku and Redis Objects is apparently looking for Redis at the local server url, even after I used the snippet above for my redis.rb.

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