title: $redis global variable with ruby on rails
body: <p>I am using redis as a read cache.  I have created an initializer</p>
  
  <p>config/initializer/redis.rb</p>
  
  <pre><code>$redis = Redis.new(:host =&gt; ENV["REDIS_HOST"], :port =&gt; ENV["REDIS_PORT"])
  </code></pre>
  
  <p>I am using this global in my unicorn.rb to create a new connection whenever a new worker is created.</p>
  
  <pre><code>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 =&gt; ENV["REDIS_HOST"], :port =&gt; ENV["REDIS_PORT"])
  end
  </code></pre>
  
  <p>I am also using this global variable whenever I need to access my redis servers.  But I am not comfortable using this global variable.  Are there any better options than using global variable?</p>