$redis global variable with ruby on rails

后端 未结 6 610
灰色年华
灰色年华 2021-02-01 14:35

I am using redis as a read cache. I have created an initializer

config/initializer/redis.rb

$redis = Redis.new(:host => ENV[\"REDIS_HOST\"], :port =&         


        
6条回答
  •  迷失自我
    2021-02-01 15:31

    try this out:-

    you can use constant instead of global variable.like in config/initializer/redis.rb

    REDIS = Redis.new(:host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"])
    

    and in unicorn.rb

    before_fork do |server, worker|
      # clear redis connection
      REDIS.quit if defined?(REDIS)
    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
    

提交回复
热议问题