首页 > 其他 > 详细

重构set redis

时间:2015-07-29 13:42:40      阅读:190      评论:0      收藏:0      [点我收藏+]

 

    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.push videos_json
end

在model的channel.rb里 

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

    needed_attributes = %w(show_id 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)
    videos_json = all_videos.map { |video| video.to_json }

    video_list.clear
    video.push videos_json
  end

 

重构set redis

原文:http://www.cnblogs.com/iwangzheng/p/4685633.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!