我会考虑两个有序集。
当搜索term
提交,获得当前timestamp
和:
zadd timestamps timestamp term
zincrby counts 1 term
,上述两个操作应该是原子的。
然后找到在给定的时间间隔timestamp_from
,timestamp_to
所有条款:
zrangebyscore timestamps timestamp_from timestamp_to
你得到这些后,环比他们从counts
得到计数。
或者,我很好奇你是否可以使用zunionstore
。下面是我在Ruby中的测试:
require 'redis'
KEYS = %w(counts timestamps results)
TERMS = %w(test0 keyword1 test0 test1 keyword1 test0 keyword0 keyword1 test0)
def redis
@redis ||= Redis.new
end
def timestamp
(Time.now.to_f * 1000).to_i
end
redis.del KEYS
TERMS.each {|term|
redis.multi {|r|
r.zadd 'timestamps', timestamp, term
r.zincrby 'counts', 1, term
}
sleep rand
}
redis.zunionstore 'results', ['timestamps', 'counts'], weights: [1, 1e15]
KEYS.each {|key|
p [key, redis.zrange(key, 0, -1, withscores: true)]
}
# top 2 terms
p redis.zrevrangebyscore 'results', '+inf', '-inf', limit: [0, 2]
编辑:在某些时候,你就需要清除counts
集。类似于@Eli提出的(https://stackoverflow.com/a/16618932/410102)。
你如何知道如何为你所做的工作指定zunionstore的参数。我正在做以下工作,'redis.zunionstore('tmp',2,'some-zest','some-set','weights',0,1,'aggregate','max')'直到我找到你:)我在哪里可以找到一些文件或相同的例子..或者我应该只是通过github上的代码(例如在github上也没有任何东西) – Akshay
@Akshay我想我从github存储库中学到了它:https ://github.com/redis/redis-rb/search?utf8 =%E2%9C%93&q = zunionstore – akonsu
明白了..谢谢:)当我查看redis.rb时,你的技术没有响个不停:p – Akshay