使用 Rails 的 custom configuration 来配置 Redis


http://guides.rubyonrails.org/configuring.html#custom-configuration


(1)config/application.rb

     # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
     
     config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
 
  
     config.i18n.default_locale = :"zh-CN"
+
+    # Rails 自定义配置
Add a comment to this line
+    # http://guides.rubyonrails.org/configuring.html#custom-configuration
+    config.x.redis.host = '127.0.0.1'
+    config.x.redis.port = 6379
   end
 end

(2)config/environments/production.rb

   # Use default logging formatter so that PID and timestamp are not suppressed.
   config.log_formatter = ::Logger::Formatter.new
+
Add a comment to this line
+  config.x.redis.host = '10.10x.xx.xx'
+  config.x.redis.port = 6379
 end

(3)config/environments/production.rb

 
   # Use default logging formatter so that PID and timestamp are not suppressed.
   config.log_formatter = ::Logger::Formatter.new
+
Add a comment to this line
+  config.x.redis.host = '10.103.xx.xx'
+  config.x.redis.port = 6379
 end

(4)config/environments/staging.rb

 # Based on production defaults
 require Rails.root.join("config/environments/production")
+
+Rails.application.configure do
Add a comment to this line
+  config.x.redis.host = '10.10x.xx.xx'
+  config.x.redis.port = 6379
+end


(5)config/initializers/redis.rb

+redis_config = Rails.configuration.x.redis
+Redis.current = Redis.new(:host => redis_config.host, :port => redis_config.port)




你可能感兴趣的:(使用 Rails 的 custom configuration 来配置 Redis)