$redis global variable with ruby on rails

后端 未结 6 611
灰色年华
灰色年华 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:21

    There is Redis.current, which you can use to store your one-and-only Redis instance.

    So instead of using $redis, you can assign your instance as follows:

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

    Redis.current was introduced to redis-rb in 2010 as a standard way to grab a redis connection, so I was surprised that no other answer mentioned it.

提交回复
热议问题