【发布时间】:2014-03-07 00:06:00
【问题描述】:
我目前正在开发一个包含查看计数器的会员网站。过去的经验表明,在 SQL 中使用视图计数器的成本很高。事实上,我一直远离查看柜台,但今天它不是一个选择。
项目使用
- Rails 4.0.2
- Redis 对象宝石
- 为了演示,我希望将 Heroku 与
Redis To Go插件一起使用 - 目前计数器基于 PG (Active Record)
- Redis 对象已用于 AR 计数(但如何保存到 AR 配置文件表?)
需要/考虑实现
- 在 Redis 中计数并可能使用 Rake + 调度任务定期存储到 PG 表中
- 效率
问题
- 我不知道如何查询 Redis DB 以查找其中的所有 Profile 对象。
如果我能得到这个对象列表,我可以编写一个 rake 任务来遍历每个项目并将模态值保存/更新到数据库。
经过一番搜索,KEYS profile:* 似乎是将所有配置文件值保存到数据库的唯一方法。然后我必须从中手动获取对象并更新值。
问题
- 正在使用
keys来查找密钥,然后查找合理的对象(就效率而言)以用于可能每天一次的计划任务。 - 有没有办法直接从 Redis 数据库中获取所有
Profile对象,就像我们可以在ActiveRecord中执行Profile.all一样? - 我们非常感谢任何关于实现计数器的建议(即使不是基于 Redis 的)
-
class Profile < ActiveRecord::Base
# To handle the profile counter
include Redis::Objects
counter :tviews
# Associations
belongs_to :user
# Other attributes
end
profiles_controller.rb
class ProfilesController < ApplicationController
def public
@profile.tviews.increment
# @profile.save # Save of AR based counting
end
end
在超薄
p.text-center#profile-counter
| Views -
= @profile.tviews.value + @profile.views
【问题讨论】:
标签: ruby-on-rails redis redis-objects