我目前正在开发一个成员网站,其中包括一个视图计数器。过去的经验表明,在SQL中使用视图计数器成本很高。事实上,我没有看到柜台,但今天这不是一个选择。
项目使用
Redis To Go插件中使用Heroku需求/实现的思考
问题
如果我能够得到这个对象列表,我可以编写一个rake任务来迭代每个项,并将模态值保存/更新到数据库中。
经过一些搜索后,KEYS profile:*似乎是获取保存到数据库中的所有配置文件值的唯一方法。然后,我必须手动获取对象并更新值。
问题
keys查找键,然后在计划的任务中使用合理的对象(就效率而言),可能每天使用一次。Profile对象,就像我们可以在ActiveRecord中执行Profile.all一样?-
class Profile < ActiveRecord::Base
# To handle the profile counter
include Redis::Objects
counter :tviews
# Associations
belongs_to :user
# Other attributes
endprofiles_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发布于 2014-03-06 16:06:00
1)不,不是。见redis文档,它说Time complexity: O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.
不仅redis必须遍历所有的键,还必须将它们存储在内存中。另外,由于redis的单线程性质,在执行key命令时,它将没有响应。
( 2)这里不是红宝石使用者,但我假设不是。
3)你有几种选择
set中,并使用定期任务(如果需要的话)清除它(以及redis事务)。这样,你就能准确地知道哪些钥匙是红色的。(redis.io现在似乎正在关闭,所以链接可能无法工作)
https://stackoverflow.com/questions/22215053
复制相似问题