redis-store / redis-actionpack

Redis stores for ActionPack
http://redis-store.org/redis-actionpack
MIT License
76 stars 44 forks source link

Support namespace option #3

Closed PikachuEXE closed 8 years ago

PikachuEXE commented 10 years ago

It takes me hours to figure out how to use namespace when using this session store

# I set this myself
Rails.configuration.redis_url #=>"redis://127.0.0.1:6379/0"

namespace = "#{Rails.application.class.parent_name.underscore}:#{Rails.env.to_s.underscore}:sessions"
url = "#{Rails.configuration.redis_url}/#{namespace}"

Rails.application.config.session_store :redis_store, {
  redis_server: url,
  key: "_#{Rails.application.class.parent_name.underscore}_#{Rails.env.to_s.underscore}_sessions_",
}

Not sure if this issue belongs here or redis-rack

PikachuEXE commented 10 years ago

oh this does not work if I have something like this:

redis://username:password@somewhere.com:port

Since it does not contain db number at the end

Now I changed to this:

# Be sure to restart your server when you modify this file.
DEFAULT_PORT = 6379

namespace = "#{Rails.application.class.parent_name.underscore}:#{Rails.env.to_s.underscore}:sessions"
uri = URI.parse(Rails.configuration.redis_url)

Rails.application.config.session_store :redis_store, {
  redis_server: {
    host: uri.host,
    port: uri.port || DEFAULT_PORT,
    password: uri.password,
    namespace: namespace,
  },
  key: "_#{Rails.application.class.parent_name.underscore}_#{Rails.env.to_s.underscore}_sessions_",
}

I am not sure if the db number in the URL is important or not So this might not be perfect

radar commented 9 years ago

@PikachuEXE Can you please confirm for me if this problem is still an issue or not?

PikachuEXE commented 9 years ago

I can reproduce the lack of namespace with the following code:

redis_url_without_namespace = "redis://127.0.0.1:6379"
namespace = "#{Rails.application.class.parent_name.underscore}:#{Rails.env.to_s.underscore}:sessions"
url = "#{redis_url_without_namespace}/#{namespace}"

Rails.application.config.session_store :redis_store, {
    redis_server: url,
    key: "_#{Rails.application.class.parent_name.underscore}_#{Rails.env.to_s.underscore}_sessions_",
}

But I am not sure if namespace can be put inside a Redis URL or not (without db number)

tubbo commented 8 years ago

@PikachuEXE are you still having issues selecting the redis namespace?

PikachuEXE commented 8 years ago

I test with the following which seem to work fine:

Rails.configuration.redis_url
#=> "redis://127.0.0.1:6379/0"
namespace = "#{Rails.application.class.parent_name.underscore}:#{Rails.env.to_s.underscore}:sessions"
url = "#{Rails.configuration.redis_url}/#{namespace}"

Rails.application.config.session_store :redis_store, {
  redis_server: url,
  key: "_#{Rails.application.class.parent_name.underscore}_#{Rails.env.to_s.underscore}_sessions_",
}