王筝的博客
ruby学习

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

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 自定义配置
+    # http://guides.rubyonrails.org/configuring.html#custom-configuration +    config.x.redis.host = '127.0.0.1'
+    config.x.redis.port = 6379
   end
 end
复制代码

config/environments/production.rb

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

 config/environments/staging.rb

复制代码
 # Based on production defaults
 require Rails.root.join("config/environments/production")
+
+Rails.application.configure do
+  config.x.redis.host = '10.103.xx.xx'
+  config.x.redis.port = 6379
+end
复制代码

config/initializers/redis.rb

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

 

 

 

 

复制代码
    def set_redis_data(channel_id,channel)
      redis = Redis.new(:host => '10.xxx.xx.xx', :port => 6379)
      redis.del(channel_id)
      yesterday = (Time.now - 36000).to_i
      all_channel_videos = channel.videos.not_deleted.where(:begin_time.gt => yesterday).desc(:begin_time)
      all_channel_videos.each do |v|
        video_hash = {}
        video_hash[:showid] = v.showid
        video_hash[:showname] = v.showname
        video_hash[:begin_time] = v.begin_time
        video_hash[:end_time] = v.end_time
        video_hash[:vid] = v.vid
        video_hash[:thumbhd] = v.thumbhd
        video_hash[:channel_id] = v.channel_id
        video_hash[:title] = v.title
        redis.lpush channel_id, video_hash.to_json
      end
    end
复制代码

在video_controller的方法

复制代码
def set_redis_data(channel)
  cache_key  = "channel_#{channel.channel_id}"
  video_list = Redis::List.new("channel_#{channel.channel_id}")

  needed_attributes = %w(show_id showname begin_time end_time vid thumbhd channel_id title)
  videos = channel.videos.active.where(:begin_time.gt => 10.hours.ago.to_i).only(needed_attributes).asc(:begin_time)
  videos_json = videos.map { |video| video.to_json }

  video_list.clear
  video_list.push *videos_json
end
复制代码

在model的channel.rb里

  def set_redis
    video_list = Redis::List.new("channel_#{channel_id}")

    needed_attributes = %w(showid duration showname begin_time end_time vid thumbhd channel_id title)
    all_videos = videos.active.where(:begin_time.gt => 10.hours.ago.to_i).only(needed_attributes).asc(:begin_time)
    video_list.clear

    unless all_videos.empty?
      videos_json = all_videos.map { |video| video.to_json }
      video_list.push *videos_json
    end
  end

 


复制代码