我使用redis作为读缓存.我创建了一个初始值设定项
config/initializer/redis.rb
$redis = Redis.new(:host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"])
我在我的独角兽身上使用这个全球定位系统.rb在创建新工作进程时创建新连接.
before_fork do |server, worker|
# clear redis connection
$redis.quit unless $redis.blank?
end
# Give each child process its own Redis connection
after_fork do |server, worker|
$redis = Redis.new(:host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"])
end
每当我需要访问redis服务器时,我也会使用这个全局变量.但我不喜欢使用这个全局变量.有比使用全局变量更好的 Select 吗?