重构set redis
2015-07-29 12:46
519 查看
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
相关文章推荐
- windows下安装redis
- redis+keepalived高可用
- centos 安装 redis
- Redis消息通知系统的实现
- 基于redis的二级缓存
- Redis相关命令
- 关于Redis五种类型对象的学习笔记
- redis的PHP封装
- php+nginx+redis安装
- NoSQL数据库:Redis适用场景及产品定位
- Redis教程4--Redis数据存储优化机制
- Redis教程3--Redis键值设计
- Redis教程2--Redis数据类型及相关命令
- Redis教程1--入门篇
- 图文介绍PHP添加Redis模块及连接
- python操作redis
- redis 快速入门实战
- redis服务启动关闭脚本
- Redis PHP 连接
- redis的安装(windows and linux)