Ruby 返回Redis实例而不是ConnectionPool的连接池
我正试图让我的Rails应用程序使用Ruby 返回Redis实例而不是ConnectionPool的连接池,ruby,ruby-on-rails-3,redis,Ruby,Ruby On Rails 3,Redis,我正试图让我的Rails应用程序使用Resque来管理员工。但是,我想继续使用ConnectionPool gem 我有一个初始化器: puts ENV["REDISTOGO_URL"] uri = (not ENV["REDISTOGO_URL"].nil?) ? URI.parse(ENV["REDISTOGO_URL"]) : nil # at this point, debugger confirms $redis is nil $redis = ConnectionPool::Wr
Resque
来管理员工。但是,我想继续使用ConnectionPool gem
我有一个初始化器:
puts ENV["REDISTOGO_URL"]
uri = (not ENV["REDISTOGO_URL"].nil?) ? URI.parse(ENV["REDISTOGO_URL"]) : nil
# at this point, debugger confirms $redis is nil
$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) {
if uri.nil?
Redis.connect
else
Redis.connect(:host => uri.host, :port => uri.port, :password => uri.password)
end
}
$redis # just put this in here for the debugger
# At this point, $redis is #<Redis:0x007fb1b0036bf0>
# when it should be an instance of ConnectionPool::Wrapper
最后,程序文件:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: env TERM_CHILD=1 QUEUE=* bundle exec rake resque:work
我正在开发环境中使用foreman
非常感谢您的帮助。文档:
您可以使用ConnectionPool::Wrapper包装单个全局连接
我认为,ConnectionPool::Wrapper
旨在包装到Redis的单个连接,以便将大型应用程序从直接使用Redis迁移到使用ConnectionPool
s
如果调用$redis.with
,则会得到由ConnectionPool
要获得实际的连接池,只需更改
ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { #redis logic }
到
在内部ConnectionPool::Wrapper创建一个普通的ConnectionPool对象,并在对包装器调用任何方法时使用方法_missing自动从该池签出/签入 这种方法的使用包括调用
检查
,或类
,或通常用于查看对象或确定其类型的任意数量的方法
需要“连接池”
类MyClass
德福
“酒吧”
结束
结束
obj=MyClass.new
对……的回答是对的
对象回答:(:用)#错误
wrapper=ConnectionPool::wrapper.new{MyClass.new}
包装器。回复:(:foo)#正确
包装纸。回复?(:with)#同样正确with'是ConnectionPool::Wrapper上的一个方法
您确实有一个ConnectionPool::Wrapper的实例,只是有点难说
ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { #redis logic }
ConnectionPool.new(:size => 5, :timeout => 3) { #redis logic }